rights reserved. Programming Dynamic AWS CloudFormation Templates Luis Colon Senior Developer Advocate AWS CloudFormation D E V 4 1 8 Anuradha Garg Sw Development Engineer AWS CloudFormation Sayali Deshpande Sw Development Manager AWS CloudFormation Chelsey Salberg Web Development Engineer AWS CloudFormation
rights reserved. Welcome! Objective: Code transformation options to make dynamic CloudFormation templates Setting up an ideal template development environment Injecting code snippets and adding utility functions Create a validation rule for your custom code
rights reserved. Before We Start… A laptop, internet, and AWS account are required Check connectivity Pair up with someone if necessary Focus on lab activities Plan to use VS Code as a local editor Retrieve all lab instructions here: https://bit.ly/2DNayik
rights reserved. CloudFormation 101 Code in YAML or JSON directly or use sample templates Upload local files or from an S3 bucket Create stack using console, API or CLI Stacks and resources are provisioned
rights reserved. Building CloudFormation Stacks A CFN template is a file that lists the resources to be created in a stack in a declarative way: tell CFN what you need created, rather than how to create it When you change your template, you are declaring the need to update your stack’s resources Virtually every kind of resource can be managed by stacks You can execute an update directly, or via an update plan called a changeset
rights reserved. Authoring Options You can use traditional IDEs or lightweight editors Some have CFN plugins: VS Code, PyCharm, others The best plugins leverage our resource specification Use utilities, like cfn-flip (converts JSON to YAML), cfn- nag, many others available on GitHub Code in higher level languages, like CDK (TypeScript), Troposphere (Python), SparkleFormation (Ruby), other options
rights reserved. CFN Linter (cfn-python-lint) Integrated with IDE’s via plugins to provide the quickest feedback on code errors, warnings Robust validation powered by our resource specification 100% Open Source - extend by building your own validation rules Use in headless mode for automated testing in pipelines
rights reserved. Our Workshop Plan LAB 2: Build and deploy a complex macro LAB 3: Create a specification to validate your macro LAB 1: Set up editor, linter and deploy a simple macro
rights reserved. Lab 1 Allotted time: around 30 minutes Objectives: Set up editor and plugins Set up CFN linter Deploy a macro Deploy a stack using the macro
rights reserved. Post Lab 1 What did we learn? Where to next: Make a new, more complex macro Deploy the macro Use the macro in a different template Deploy the stack
rights reserved. CloudFormation Macros Enables template coders to write short-hand, abbreviated instructions that expand automatically when deployed Add utility functions, for example, iteration loops, strings, etc. Ensure resources are defined to comply to your standards Easy to share and reuse across stacks 3 step process
rights reserved. Macro Example 4: Add Global Variables Transform: Globals Globals: SomeText: some-text ThingTag: Key: Thing Value: This is a thing Resources: Bucket: Type: AWS::S3::Bucket Properties: BucketName: "@SomeText" Tags: - "@ThingTag" - Key: OtherThing Value: Other thing value Resources: Bucket: Type: AWS::S3::Bucket Properties: BucketName: “some-text" Tags: - Key: Thing Value: This is a thing - Key: OtherThing Value: Other thing value
rights reserved. Lab 2 Allotted time: around 30 minutes Objectives: Make a new macro Deploy the macro Use the macro in a different template Deploy the stack that uses the macro
rights reserved. CFN Linter To install: (requires Python) Download: https://github.com/awslabs/cfn-python- lint Run: pip install cfn-lint To try: cfn-lint –t simple-vpc.yaml • Process multiple files • Better handling of Conditions/Fn::If • SAM Local integration for SAM templates • CloudFormation limit checks • Service rules for Route53 and CodePipeline • Used in other tools, like TaskCat GitHub 306 Stars 290 PRs merged 153 Issues closed 61 Forks Current release: v.0.9.1 Pypi 6,905 installs this week 23,232 installs this month 55,871 installs since release
rights reserved. Linting your own macro The linter knows about all resources known to CloudFormation, via the resource specification Strategy: to lint your own macros, you override the CloudFormation resource specification
rights reserved. What we’re doing Consider the following template: The Type AWS::S3::Object is not in the CloudFormation resource spec! --- AWSTemplateFormatVersion: '2010-09-09' Description: "Create an S3 Object" Parameters: TargetBucket: Type: "String" Default: "foo" Resources: NewObject: Type: AWS::S3::Object Properties: Target: Bucket: !Ref TargetBucket Key: README.md Body: | # My text file This is my text file; there are many like it, but this one is mine.
rights reserved. Lab 3 Allotted time: around 30 minutes Objectives: Running a simple validation via command line Validating against a macro Overriding CloudFormation specifications Re-validating the macro with the new specifications Bonus! Validate the macro you worked with on Lab 2
rights reserved. Wrap Up Leverage macros for many use cases Create a specification to validate your macro Set up optimal tools to improve your productivity
affiliates. All rights reserved. Luis Colon Senior Developer Advocate AWS CloudFormation Anuradha Garg Sw Development Engineer AWS CloudFormation Sayali Deshpande Sw Development Manager AWS CloudFormation Chelsey Salberg Web Development Engineer AWS CloudFormation