mirror of
https://github.com/farcasclaudiu/terraform-course.git
synced 2026-06-22 05:01:55 +03:00
cdktf-go
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
# cdktf and terraform ignores
|
||||||
|
.terraform
|
||||||
|
cdktf.out
|
||||||
|
cdktf.log
|
||||||
|
*terraform.*.tfstate*
|
||||||
|
generated
|
||||||
|
|
||||||
|
# src https://github.com/github/gitignore/blob/master/Go.gitignore
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
# Running the cdktf-go example
|
||||||
|
|
||||||
|
## Install dependencies
|
||||||
|
1. Install an IDE like VSCode (https://code.visualstudio.com/). Then
|
||||||
|
2. Install golang, terraform, cdktf:
|
||||||
|
|
||||||
|
On MacOS:
|
||||||
|
```
|
||||||
|
brew install go cdktf terraform
|
||||||
|
```
|
||||||
|
|
||||||
|
On Windows / Mac / Linux with vagrant:
|
||||||
|
```
|
||||||
|
git clone https://github.com/wardviaene/devops-box
|
||||||
|
cd devops-box
|
||||||
|
vagrant up
|
||||||
|
vagrant ssh
|
||||||
|
/vagrant/scripts/install_cdktf.sh # kicks of install of golang / npm / cdktf
|
||||||
|
```
|
||||||
|
|
||||||
|
On Windows, install a package manager (https://chocolatey.org/), then:
|
||||||
|
```
|
||||||
|
choco install nodejs
|
||||||
|
npm install --global cdktf-cli@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
# Download & init the provider
|
||||||
|
|
||||||
|
```
|
||||||
|
cdktf get
|
||||||
|
go mod tidy
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: can take a some time to finish
|
||||||
|
|
||||||
|
# Provision
|
||||||
|
|
||||||
|
This provisions the infrastructure on AWS.
|
||||||
|
|
||||||
|
```
|
||||||
|
cdktf deploy
|
||||||
|
```
|
||||||
|
|
||||||
|
# Initialize new project
|
||||||
|
If you'd like to initialize your own project, you can use this command (I used the same command to initialize this project):
|
||||||
|
```
|
||||||
|
cdktf init --template="go" --local
|
||||||
|
```
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"language": "go",
|
||||||
|
"app": "go run main.go",
|
||||||
|
"codeMakerOutput": "generated",
|
||||||
|
"projectId": "1f093b2f-3904-471b-984a-a6efe009927e",
|
||||||
|
"terraformProviders": [
|
||||||
|
"hashicorp/aws@~> 3.67.0"
|
||||||
|
],
|
||||||
|
"terraformModules": [],
|
||||||
|
"context": {
|
||||||
|
"excludeStackIdFromLogicalIds": "true",
|
||||||
|
"allowSepCharsInLogicalIds": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
module cdk.tf/go/stack
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require github.com/aws/constructs-go/constructs/v10 v10.0.25
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/aws/jsii-runtime-go v1.50.0
|
||||||
|
github.com/hashicorp/terraform-cdk-go/cdktf v0.8.6
|
||||||
|
)
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
|
||||||
|
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
|
||||||
|
github.com/aws/constructs-go/constructs/v10 v10.0.25 h1:yY4u3qgRs99+OqNXNwS6BPazHN+Tr7ttJSlpbFHoM98=
|
||||||
|
github.com/aws/constructs-go/constructs/v10 v10.0.25/go.mod h1:Tk3teOB9AAvXsgGcl3MLrOv0iTSv4UTQStQ5TQ+OIJc=
|
||||||
|
github.com/aws/jsii-runtime-go v1.50.0 h1:HT96p8bIkG4Be56pgHUeN+QydnGJTB+VBYd6ouVBWN0=
|
||||||
|
github.com/aws/jsii-runtime-go v1.50.0/go.mod h1:6tZnlstx8bAB3vnLFF9n8bbkI//LDblAek9zFyMXV3E=
|
||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/hashicorp/terraform-cdk-go/cdktf v0.8.6 h1:ClziHluDMDHjG2DEYaePZwnHAlYMiiTCgQJ2YF6Bpc4=
|
||||||
|
github.com/hashicorp/terraform-cdk-go/cdktf v0.8.6/go.mod h1:j+G48UF4TkctRpf9SQ1mMy7fIwP8nc+DYWumI249BqY=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/aws/constructs-go/constructs/v10"
|
||||||
|
"github.com/aws/jsii-runtime-go"
|
||||||
|
"github.com/hashicorp/terraform-cdk-go/cdktf"
|
||||||
|
|
||||||
|
"cdk.tf/go/stack/generated/hashicorp/aws"
|
||||||
|
"cdk.tf/go/stack/generated/hashicorp/aws/ec2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
|
||||||
|
stack := cdktf.NewTerraformStack(scope, &id)
|
||||||
|
|
||||||
|
// The code that defines your stack goes here
|
||||||
|
|
||||||
|
aws.NewAwsProvider(stack, jsii.String("aws"), &aws.AwsProviderConfig{
|
||||||
|
Region: jsii.String("eu-west-1"),
|
||||||
|
})
|
||||||
|
|
||||||
|
amiID := getUbuntuAMI(stack)
|
||||||
|
|
||||||
|
instance := ec2.NewInstance(stack, jsii.String("compute"), &ec2.InstanceConfig{
|
||||||
|
Ami: amiID.Id(),
|
||||||
|
InstanceType: jsii.String("t2.micro"),
|
||||||
|
})
|
||||||
|
|
||||||
|
cdktf.NewTerraformOutput(stack, jsii.String("public_ip"), &cdktf.TerraformOutputConfig{
|
||||||
|
Value: instance.PublicIp(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return stack
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := cdktf.NewApp(nil)
|
||||||
|
|
||||||
|
NewMyStack(app, "cdktf-go")
|
||||||
|
|
||||||
|
app.Synth()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUbuntuAMI(stack cdktf.TerraformStack) ec2.DataAwsAmi {
|
||||||
|
return ec2.NewDataAwsAmi(stack, jsii.String("computeAMI"), &ec2.DataAwsAmiConfig{
|
||||||
|
Filter: &[]*ec2.DataAwsAmiFilter{
|
||||||
|
{
|
||||||
|
Name: jsii.String("name"),
|
||||||
|
Values: jsii.Strings("ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: jsii.String("virtualization-type"),
|
||||||
|
Values: jsii.Strings("hvm"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Owners: jsii.Strings("099720109477"),
|
||||||
|
MostRecent: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user