cloudformationinclude

package
v1.168.0-devpreview Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Include CloudFormation templates in the CDK

This module contains a set of classes whose goal is to facilitate working with existing CloudFormation templates in the CDK. It can be thought of as an extension of the capabilities of the CfnInclude class.

Basic usage

Assume we have a file with an existing template. It could be in JSON format, in a file my-template.json:

{
  "Resources": {
    "Bucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "some-bucket-name"
      }
    }
  }
}

Or it could by in YAML format, in a file my-template.yaml:

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: some-bucket-name

It can be included in a CDK application with the following code:

cfnTemplate := cfn_inc.NewCfnInclude(this, jsii.String("Template"), &cfnIncludeProps{
	templateFile: jsii.String("my-template.json"),
})

Or, if your template uses YAML:

cfnTemplate := cfn_inc.NewCfnInclude(this, jsii.String("Template"), &cfnIncludeProps{
	templateFile: jsii.String("my-template.yaml"),
})

Note: different YAML parsers sometimes don't agree on what exactly constitutes valid YAML. If you get a YAML exception when including your template, try converting it to JSON, and including that file instead. If you're downloading your template from the CloudFormation AWS Console, you can easily get it in JSON format by clicking the 'View in Designer' button on the 'Template' tab - once in Designer, select JSON in the "Choose template language" radio buttons on the bottom pane.

This will add all resources from my-template.json / my-template.yaml into the CDK application, preserving their original logical IDs from the template file.

Note that this including process will not execute any CloudFormation transforms - including the Serverless transform.

Any resource from the included template can be retrieved by referring to it by its logical ID from the template. If you know the class of the CDK object that corresponds to that resource, you can cast the returned object to the correct type:

var cfnTemplate cfnInclude

cfnBucket := cfnTemplate.getResource(jsii.String("Bucket")).(cfnBucket)

Note that any resources not present in the latest version of the CloudFormation schema at the time of publishing the version of this module that you depend on, including Custom Resources, will be returned as instances of the class CfnResource, and so cannot be cast to a different resource type.

Any modifications made to that resource will be reflected in the resulting CDK template; for example, the name of the bucket can be changed:

var cfnTemplate cfnInclude

cfnBucket := cfnTemplate.getResource(jsii.String("Bucket")).(cfnBucket)
cfnBucket.bucketName = "my-bucket-name"

You can also refer to the resource when defining other constructs, including the higher-level ones (those whose name does not start with Cfn), for example:

var cfnTemplate cfnInclude

cfnBucket := cfnTemplate.getResource(jsii.String("Bucket")).(cfnBucket)

role := iam.NewRole(this, jsii.String("Role"), &roleProps{
	assumedBy: iam.NewAnyPrincipal(),
})
role.addToPolicy(iam.NewPolicyStatement(&policyStatementProps{
	actions: []*string{
		jsii.String("s3:*"),
	},
	resources: []*string{
		cfnBucket.attrArn,
	},
}))
Converting L1 resources to L2

The resources the getResource method returns are what the CDK calls Layer 1 resources (like CfnBucket). However, in many places in the Construct Library, the CDK requires so-called Layer 2 resources, like IBucket. There are two ways of going from an L1 to an L2 resource.

UsingfromCfn*() methods

This is the preferred method of converting an L1 resource to an L2. It works by invoking a static method of the class of the L2 resource whose name starts with fromCfn - for example, for KMS Keys, that would be the Kms.fromCfnKey() method - and passing the L1 instance as an argument:

var cfnTemplate cfnInclude

cfnKey := cfnTemplate.getResource(jsii.String("Key")).(cfnKey)
key := kms.key.fromCfnKey(cfnKey)

This returns an instance of the kms.IKey type that can be passed anywhere in the CDK an IKey is expected. What is more, that IKey instance will be mutable - which means calling any mutating methods on it, like addToResourcePolicy(), will be reflected in the resulting template.

Note that, in some cases, the fromCfn*() method might not be able to create an L2 from the underlying L1. This can happen when the underlying L1 heavily uses CloudFormation functions. For example, if you tried to create an L2 IKey from an L1 represented as this CloudFormation template:

{
  "Resources": {
    "Key": {
      "Type": "AWS::KMS::Key",
      "Properties": {
        "KeyPolicy": {
          "Statement": [
            {
              "Fn::If": [
                "Condition",
                {
                  "Action": "kms:if-action",
                  "Resource": "*",
                  "Principal": "*",
                  "Effect": "Allow"
                },
                {
                  "Action": "kms:else-action",
                  "Resource": "*",
                  "Principal": "*",
                  "Effect": "Allow"
                }
              ]
            }
          ],
          "Version": "2012-10-17"
        }
      }
    }
  }
}

The Key.fromCfnKey() method does not know how to translate that into CDK L2 concepts, and would throw an exception.

In those cases, you need the use the second method of converting an L1 to an L2.

Using from*Name/Arn/Attributes() methods

If the resource you need does not have a fromCfn*() method, or if it does, but it throws an exception for your particular L1, you need to use the second method of converting an L1 resource to L2.

Each L2 class has static factory methods with names like from*Name(), from*Arn(), and/or from*Attributes(). You can obtain an L2 resource from an L1 by passing the correct properties of the L1 as the arguments to those methods:

var cfnTemplate cfnInclude

// using from*Attributes()
var privateCfnSubnet1 cfnSubnet
var privateCfnSubnet2 cfnSubnet


// using from*Name()
cfnBucket := cfnTemplate.getResource(jsii.String("Bucket")).(cfnBucket)
bucket := s3.bucket.fromBucketName(this, jsii.String("L2Bucket"), cfnBucket.ref)

// using from*Arn()
cfnKey := cfnTemplate.getResource(jsii.String("Key")).(cfnKey)
key := kms.key.fromKeyArn(this, jsii.String("L2Key"), cfnKey.attrArn)
cfnVpc := cfnTemplate.getResource(jsii.String("Vpc")).(cfnVPC)
vpc := ec2.vpc.fromVpcAttributes(this, jsii.String("L2Vpc"), &vpcAttributes{
	vpcId: cfnVpc.ref,
	availabilityZones: core.fn.getAzs(),
	privateSubnetIds: []*string{
		privateCfnSubnet1.ref,
		privateCfnSubnet2.ref,
	},
})

As long as they just need to be referenced, and not changed in any way, everything should work; however, note that resources returned from those methods, unlike those returned by fromCfn*() methods, are immutable, which means calling any mutating methods on them will have no effect. You will have to mutate the underlying L1 in order to change them.

Non-resource template elements

In addition to resources, you can also retrieve and mutate all other template elements:

  • Parameters:

    var cfnTemplate cfnInclude
    
    param := cfnTemplate.getParameter(jsii.String("MyParameter"))
    
    // mutating the parameter
    param.default = "MyDefault"
    
  • Conditions:

    var cfnTemplate cfnInclude
    
    condition := cfnTemplate.getCondition(jsii.String("MyCondition"))
    
    // mutating the condition
    condition.expression = core.fn.conditionEquals(jsii.Number(1), jsii.Number(2))
    
  • Mappings:

    var cfnTemplate cfnInclude
    
    mapping := cfnTemplate.getMapping(jsii.String("MyMapping"))
    
    // mutating the mapping
    mapping.setValue(jsii.String("my-region"), jsii.String("AMI"), jsii.String("ami-04681a1dbd79675a5"))
    
  • Service Catalog template Rules:

    var cfnTemplate cfnInclude
    
    // mutating the rule
    var myParameter cfnParameter
    
    rule := cfnTemplate.getRule(jsii.String("MyRule"))
    rule.addAssertion(core.fn.conditionContains([]*string{
    	jsii.String("m1.small"),
    }, myParameter.valueAsString), jsii.String("MyParameter has to be m1.small"))
    
  • Outputs:

    var cfnTemplate cfnInclude
    
    // mutating the output
    var cfnBucket cfnBucket
    
    output := cfnTemplate.getOutput(jsii.String("MyOutput"))
    output.value = cfnBucket.attrArn
    
  • Hooks for blue-green deployments:

    var cfnTemplate cfnInclude
    
    // mutating the hook
    var myRole role
    
    hook := cfnTemplate.getHook(jsii.String("MyOutput"))
    codeDeployHook := hook.(cfnCodeDeployBlueGreenHook)
    codeDeployHook.serviceRole = myRole.roleArn
    

Parameter replacement

If your existing template uses CloudFormation Parameters, you may want to remove them in favor of build-time values. You can do that using the parameters property:

cfn_inc.NewCfnInclude(this, jsii.String("includeTemplate"), &cfnIncludeProps{
	templateFile: jsii.String("path/to/my/template"),
	parameters: map[string]interface{}{
		"MyParam": jsii.String("my-value"),
	},
})

This will replace all references to MyParam with the string 'my-value', and MyParam will be removed from the 'Parameters' section of the resulting template.

Nested Stacks

This module also supports templates that use nested stacks.

For example, if you have the following parent template:

{
  "Resources": {
    "ChildStack": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "https://my-s3-template-source.s3.amazonaws.com/child-stack.json"
      }
    }
  }
}

where the child template pointed to by https://my-s3-template-source.s3.amazonaws.com/child-stack.json is:

{
  "Resources": {
    "MyBucket": {
      "Type": "AWS::S3::Bucket"
    }
  }
}

You can include both the parent stack, and the nested stack in your CDK application as follows:

parentTemplate := cfn_inc.NewCfnInclude(this, jsii.String("ParentStack"), &cfnIncludeProps{
	templateFile: jsii.String("path/to/my-parent-template.json"),
	loadNestedStacks: map[string]*cfnIncludeProps{
		"ChildStack": &cfnIncludeProps{
			"templateFile": jsii.String("path/to/my-nested-template.json"),
		},
	},
})

Here, path/to/my-nested-template.json represents the path on disk to the downloaded template file from the original template URL of the nested stack (https://my-s3-template-source.s3.amazonaws.com/child-stack.json). In the CDK application, this file will be turned into an Asset, and the TemplateURL property of the nested stack resource will be modified to point to that asset.

The included nested stack can be accessed with the getNestedStack method:

var parentTemplate cfnInclude


includedChildStack := parentTemplate.getNestedStack(jsii.String("ChildStack"))
childStack := includedChildStack.stack
childTemplate := includedChildStack.includedTemplate

Now you can reference resources from ChildStack, and modify them like any other included template:

var childTemplate cfnInclude


cfnBucket := childTemplate.getResource(jsii.String("MyBucket")).(cfnBucket)
cfnBucket.bucketName = "my-new-bucket-name"

role := iam.NewRole(this, jsii.String("MyRole"), &roleProps{
	assumedBy: iam.NewAccountRootPrincipal(),
})

role.addToPolicy(iam.NewPolicyStatement(&policyStatementProps{
	actions: []*string{
		jsii.String("s3:GetObject*"),
		jsii.String("s3:GetBucket*"),
		jsii.String("s3:List*"),
	},
	resources: []*string{
		cfnBucket.attrArn,
	},
}))

You can also include the nested stack after the CfnInclude object was created, instead of doing it on construction:

var parentTemplate cfnInclude

includedChildStack := parentTemplate.loadNestedStack(jsii.String("ChildTemplate"), &cfnIncludeProps{
	templateFile: jsii.String("path/to/my-nested-template.json"),
})

Vending CloudFormation templates as Constructs

In many cases, there are existing CloudFormation templates that are not entire applications, but more like specialized fragments, implementing a particular pattern or best practice. If you have templates like that, you can use the CfnInclude class to vend them as CDK Constructs:

import "github.com/aws/constructs-go/constructs"
import cfn_inc "github.com/aws/aws-cdk-go/awscdk"
import path "github.com/aws-samples/dummy/path"

type MyConstruct struct {
	construct
}

func NewMyConstruct(scope construct, id *string) *MyConstruct {
	this := &MyConstruct{}
	newConstruct_Override(this, scope, id)

	// include a template inside the Construct
	// include a template inside the Construct
	cfn_inc.NewCfnInclude(this, jsii.String("MyConstruct"), &cfnIncludeProps{
		templateFile: path.join(__dirname, jsii.String("my-template.json")),
		preserveLogicalIds: jsii.Boolean(false),
	})
	return this
}

Notice the preserveLogicalIds parameter - it makes sure the logical IDs of all the included template elements are re-named using CDK's algorithm, guaranteeing they are unique within your application. Without that parameter passed, instantiating MyConstruct twice in the same Stack would result in duplicated logical IDs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CfnInclude_IsCfnElement

func CfnInclude_IsCfnElement(x interface{}) *bool

Returns `true` if a construct is a stack element (i.e. part of the synthesized cloudformation template).

Uses duck-typing instead of `instanceof` to allow stack elements from different versions of this library to be included in the same stack.

Returns: The construct as a stack element or undefined if it is not a stack element. Experimental.

func CfnInclude_IsConstruct

func CfnInclude_IsConstruct(x interface{}) *bool

Return whether the given object is a Construct. Experimental.

func NewCfnInclude_Override

func NewCfnInclude_Override(c CfnInclude, scope constructs.Construct, id *string, props *CfnIncludeProps)

Experimental.

Types

type CfnInclude

type CfnInclude interface {
	awscdk.CfnElement
	// Returns: the stack trace of the point where this Resource was created from, sourced
	// from the +metadata+ entry typed +aws:cdk:logicalId+, and with the bottom-most
	// node +internal+ entries filtered.
	// Experimental.
	CreationStack() *[]*string
	// The logical ID for this CloudFormation stack element.
	//
	// The logical ID of the element
	// is calculated from the path of the resource node in the construct tree.
	//
	// To override this value, use `overrideLogicalId(newLogicalId)`.
	//
	// Returns: the logical ID as a stringified token. This value will only get
	// resolved during synthesis.
	// Experimental.
	LogicalId() *string
	// The construct tree node associated with this construct.
	// Experimental.
	Node() awscdk.ConstructNode
	// The stack in which this element is defined.
	//
	// CfnElements must be defined within a stack scope (directly or indirectly).
	// Experimental.
	Stack() awscdk.Stack
	// Returns the CfnCondition object from the 'Conditions' section of the CloudFormation template with the given name.
	//
	// Any modifications performed on that object will be reflected in the resulting CDK template.
	//
	// If a Condition with the given name is not present in the template,
	// throws an exception.
	// Experimental.
	GetCondition(conditionName *string) awscdk.CfnCondition
	// Returns the CfnHook object from the 'Hooks' section of the included CloudFormation template with the given logical ID.
	//
	// Any modifications performed on the returned object will be reflected in the resulting CDK template.
	//
	// If a Hook with the given logical ID is not present in the template,
	// an exception will be thrown.
	// Experimental.
	GetHook(hookLogicalId *string) awscdk.CfnHook
	// Returns the CfnMapping object from the 'Mappings' section of the included template.
	//
	// Any modifications performed on that object will be reflected in the resulting CDK template.
	//
	// If a Mapping with the given name is not present in the template,
	// an exception will be thrown.
	// Experimental.
	GetMapping(mappingName *string) awscdk.CfnMapping
	// Returns a loaded NestedStack with name logicalId.
	//
	// For a nested stack to be returned by this method,
	// it must be specified either in the {@link CfnIncludeProps.loadNestedStacks} property,
	// or through the {@link loadNestedStack} method.
	// Experimental.
	GetNestedStack(logicalId *string) *IncludedNestedStack
	// Returns the CfnOutput object from the 'Outputs' section of the included template.
	//
	// Any modifications performed on that object will be reflected in the resulting CDK template.
	//
	// If an Output with the given name is not present in the template,
	// throws an exception.
	// Experimental.
	GetOutput(logicalId *string) awscdk.CfnOutput
	// Returns the CfnParameter object from the 'Parameters' section of the included template.
	//
	// Any modifications performed on that object will be reflected in the resulting CDK template.
	//
	// If a Parameter with the given name is not present in the template,
	// throws an exception.
	// Experimental.
	GetParameter(parameterName *string) awscdk.CfnParameter
	// Returns the low-level CfnResource from the template with the given logical ID.
	//
	// Any modifications performed on that resource will be reflected in the resulting CDK template.
	//
	// The returned object will be of the proper underlying class;
	// you can always cast it to the correct type in your code:
	//
	//      // assume the template contains an AWS::S3::Bucket with logical ID 'Bucket'
	//      const cfnBucket = cfnTemplate.getResource('Bucket') as s3.CfnBucket;
	//      // cfnBucket is of type s3.CfnBucket
	//
	// If the template does not contain a resource with the given logical ID,
	// an exception will be thrown.
	// Experimental.
	GetResource(logicalId *string) awscdk.CfnResource
	// Returns the CfnRule object from the 'Rules' section of the CloudFormation template with the given name.
	//
	// Any modifications performed on that object will be reflected in the resulting CDK template.
	//
	// If a Rule with the given name is not present in the template,
	// an exception will be thrown.
	// Experimental.
	GetRule(ruleName *string) awscdk.CfnRule
	// Includes a template for a child stack inside of this parent template.
	//
	// A child with this logical ID must exist in the template,
	// and be of type AWS::CloudFormation::Stack.
	// This is equivalent to specifying the value in the {@link CfnIncludeProps.loadNestedStacks}
	// property on object construction.
	//
	// Returns: the same {@link IncludedNestedStack} object that {@link getNestedStack} returns for this logical ID.
	// Experimental.
	LoadNestedStack(logicalId *string, nestedStackProps *CfnIncludeProps) *IncludedNestedStack
	// Perform final modifications before synthesis.
	//
	// This method can be implemented by derived constructs in order to perform
	// final changes before synthesis. prepare() will be called after child
	// constructs have been prepared.
	//
	// This is an advanced framework feature. Only use this if you
	// understand the implications.
	// Experimental.
	OnPrepare()
	// Allows this construct to emit artifacts into the cloud assembly during synthesis.
	//
	// This method is usually implemented by framework-level constructs such as `Stack` and `Asset`
	// as they participate in synthesizing the cloud assembly.
	// Experimental.
	OnSynthesize(session constructs.ISynthesisSession)
	// Validate the current construct.
	//
	// This method can be implemented by derived constructs in order to perform
	// validation logic. It is called on all constructs before synthesis.
	//
	// Returns: An array of validation error messages, or an empty array if the construct is valid.
	// Experimental.
	OnValidate() *[]*string
	// Overrides the auto-generated logical ID with a specific ID.
	// Experimental.
	OverrideLogicalId(newLogicalId *string)
	// Perform final modifications before synthesis.
	//
	// This method can be implemented by derived constructs in order to perform
	// final changes before synthesis. prepare() will be called after child
	// constructs have been prepared.
	//
	// This is an advanced framework feature. Only use this if you
	// understand the implications.
	// Experimental.
	Prepare()
	// Allows this construct to emit artifacts into the cloud assembly during synthesis.
	//
	// This method is usually implemented by framework-level constructs such as `Stack` and `Asset`
	// as they participate in synthesizing the cloud assembly.
	// Experimental.
	Synthesize(session awscdk.ISynthesisSession)
	// Returns a string representation of this construct.
	// Experimental.
	ToString() *string
	// Validate the current construct.
	//
	// This method can be implemented by derived constructs in order to perform
	// validation logic. It is called on all constructs before synthesis.
	//
	// Returns: An array of validation error messages, or an empty array if the construct is valid.
	// Experimental.
	Validate() *[]*string
}

Construct to import an existing CloudFormation template file into a CDK application.

All resources defined in the template file can be retrieved by calling the {@link getResource} method. Any modifications made on the returned resource objects will be reflected in the resulting CDK template.

Example:

cfnTemplate := cfn_inc.NewCfnInclude(this, jsii.String("Template"), &cfnIncludeProps{
	templateFile: jsii.String("my-template.json"),
})

Experimental.

func NewCfnInclude

func NewCfnInclude(scope constructs.Construct, id *string, props *CfnIncludeProps) CfnInclude

Experimental.

type CfnIncludeProps

type CfnIncludeProps struct {
	// Path to the template file.
	//
	// Both JSON and YAML template formats are supported.
	// Experimental.
	TemplateFile *string `field:"required" json:"templateFile" yaml:"templateFile"`
	// Specifies the template files that define nested stacks that should be included.
	//
	// If your template specifies a stack that isn't included here, it won't be created as a NestedStack
	// resource, and it won't be accessible from the {@link CfnInclude.getNestedStack} method
	// (but will still be accessible from the {@link CfnInclude.getResource} method).
	//
	// If you include a stack here with an ID that isn't in the template,
	// or is in the template but is not a nested stack,
	// template creation will fail and an error will be thrown.
	// Experimental.
	LoadNestedStacks *map[string]*CfnIncludeProps `field:"optional" json:"loadNestedStacks" yaml:"loadNestedStacks"`
	// Specifies parameters to be replaced by the values in this mapping.
	//
	// Any parameters in the template that aren't specified here will be left unmodified.
	// If you include a parameter here with an ID that isn't in the template,
	// template creation will fail and an error will be thrown.
	// Experimental.
	Parameters *map[string]interface{} `field:"optional" json:"parameters" yaml:"parameters"`
	// Whether the resources should have the same logical IDs in the resulting CDK template as they did in the original CloudFormation template file.
	//
	// If you're vending a Construct using an existing CloudFormation template,
	// make sure to pass this as `false`.
	//
	// **Note**: regardless of whether this option is true or false,
	// the {@link CfnInclude.getResource} and related methods always uses the original logical ID of the resource/element,
	// as specified in the template file.
	// Experimental.
	PreserveLogicalIds *bool `field:"optional" json:"preserveLogicalIds" yaml:"preserveLogicalIds"`
}

Construction properties of {@link CfnInclude}.

Example:

parentTemplate := cfn_inc.NewCfnInclude(this, jsii.String("ParentStack"), &cfnIncludeProps{
	templateFile: jsii.String("path/to/my-parent-template.json"),
	loadNestedStacks: map[string]*cfnIncludeProps{
		"ChildStack": &cfnIncludeProps{
			"templateFile": jsii.String("path/to/my-nested-template.json"),
		},
	},
})

Experimental.

type IncludedNestedStack

type IncludedNestedStack struct {
	// The CfnInclude that represents the template, which can be used to access Resources and other template elements.
	// Experimental.
	IncludedTemplate CfnInclude `field:"required" json:"includedTemplate" yaml:"includedTemplate"`
	// The NestedStack object which represents the scope of the template.
	// Experimental.
	Stack awscdk.NestedStack `field:"required" json:"stack" yaml:"stack"`
}

The type returned from {@link CfnInclude.getNestedStack}. Contains both the NestedStack object and CfnInclude representations of the child stack.

Example:

var parentTemplate cfnInclude

includedChildStack := parentTemplate.getNestedStack(jsii.String("ChildStack"))
childStack := includedChildStack.stack
childTemplate := includedChildStack.includedTemplate

Experimental.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL