Create Instance

Generated constructs

The ec2 construct is included in the generated subdirectory.

generated/hashicorp/aws/ec2
├── ec2.go
├── ec2.init.go
└── internal
    └── types.go

Define instance

Now you can code the instance:

package main

import (
    "cdk.tf/go/stack/generated/hashicorp/aws"
    "cdk.tf/go/stack/generated/hashicorp/aws/ec2"

    "github.com/aws/constructs-go/constructs/v10"
    "github.com/aws/jsii-runtime-go"
    "github.com/hashicorp/terraform-cdk-go/cdktf"
)

func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
    stack := cdktf.NewTerraformStack(scope, &id)

    aws.NewAwsProvider(stack, jsii.String("aws"), &aws.AwsProviderConfig{
        Region: jsii.String("eu-central-1"),
    })

    instance := ec2.NewInstance(stack, jsii.String("cdktfgo"), &ec2.InstanceConfig{
        Ami:          jsii.String("ami-04c921614424b07cd"),
        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, "learn-cdktf-go")

    app.Synth()
}

  1 package main
  2
  3 import (
  4     "cdk.tf/go/stack/generated/hashicorp/aws"
  5     "cdk.tf/go/stack/generated/hashicorp/aws/ec2"
  6
  7     "github.com/aws/constructs-go/constructs/v10"
  8     "github.com/aws/jsii-runtime-go"
  9     "github.com/hashicorp/terraform-cdk-go/cdktf"
 10 )
 11
 12 func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
 13     stack := cdktf.NewTerraformStack(scope, &id)
 14
 15     aws.NewAwsProvider(stack, jsii.String("aws"), &aws.AwsProviderConfig{
 16         Region: jsii.String("eu-central-1"),
 17     })
 18
 19     instance := ec2.NewInstance(stack, jsii.String("cdktfgo"), &ec2.InstanceConfig{
 20         Ami:          jsii.String("ami-04c921614424b07cd"),
 21         InstanceType: jsii.String("t2.micro"),
 22     })
 23
 24     cdktf.NewTerraformOutput(stack, jsii.String("public_ip"), &cdktf.TerraformOutputConfig{
 25         Value: instance.PublicIp(),
 26     })
 27
 28     return stack
 29 }
 30
 31 func main() {
 32     app := cdktf.NewApp(nil)
 33
 34     NewMyStack(app, "learn-cdktf-go")
 35
 36     app.Synth()
 37 }

Sections of main.go

EC2 instance

The generated constructs are imported in line 5:

  5     "cdk.tf/go/stack/generated/hashicorp/aws/ec2"

With that you can create an terraform instance construct in line 19-22.

 19     instance := ec2.NewInstance(stack, jsii.String("cdktfgo"), &ec2.InstanceConfig{
 20         Ami:          jsii.String("ami-04c921614424b07cd"),
 21         InstanceType: jsii.String("t2.micro"),
 22     })

Stack generation

 31 func main() {
 32     app := cdktf.NewApp(nil)
 33
 34     NewMyStack(app, "learn-cdktf-go")
 35
 36     app.Synth()
 37 }

Now you can call the NewMyStack function to create an HCL file.

See the apply-destroy cycle in the next chapter.

Typo in hashicorp example

In the example the instance ist created from aws, which is wrong:

instance := aws.NewInstance...

See also