0
0
AWScloud~5 mins

Template structure (JSON/YAML) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
AWS CloudFormation templates let you describe your cloud resources in a file. This file can be in JSON or YAML format. It helps you create and manage AWS resources automatically and consistently.
When you want to create multiple AWS resources together as one unit.
When you need to keep your infrastructure setup in a file for easy updates and sharing.
When you want to automate the setup of your cloud environment without clicking in the console.
When you want to ensure your infrastructure is the same every time you deploy.
When you want to track changes to your cloud setup using version control.
Config File - template.yaml
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple AWS CloudFormation template to create an S3 bucket
Resources:
  MyS3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: example-cloudformation-bucket-12345

AWSTemplateFormatVersion: Specifies the template version.

Description: A short explanation of what this template does.

Resources: The AWS resources to create. Here, it creates an S3 bucket named 'example-cloudformation-bucket-12345'.

Commands
This command checks if the CloudFormation template file is valid before creating resources.
Terminal
aws cloudformation validate-template --template-body file://template.yaml
Expected OutputExpected
{ "Parameters": [] }
--template-body - Specifies the path to the template file.
This command creates a new CloudFormation stack named 'example-stack' using the template file.
Terminal
aws cloudformation create-stack --stack-name example-stack --template-body file://template.yaml
Expected OutputExpected
{ "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/example-stack/abcd1234-5678-90ef-ghij-klmnopqrstuv" }
--stack-name - Names the stack to create.
--template-body - Specifies the template file to use.
This command shows details about the created stack, including its status and resources.
Terminal
aws cloudformation describe-stacks --stack-name example-stack
Expected OutputExpected
{ "Stacks": [ { "StackName": "example-stack", "StackStatus": "CREATE_COMPLETE", "CreationTime": "2024-06-01T12:00:00.000Z", "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/example-stack/abcd1234-5678-90ef-ghij-klmnopqrstuv" } ] }
--stack-name - Specifies which stack to describe.
Key Concept

If you remember nothing else from this pattern, remember: AWS CloudFormation templates define your cloud resources in a file so you can create and manage them automatically and consistently.

Common Mistakes
Using incorrect indentation or syntax in the YAML or JSON template.
CloudFormation templates must follow strict syntax rules; otherwise, validation and deployment fail.
Use a YAML or JSON validator and the 'aws cloudformation validate-template' command before deploying.
Not specifying the full path with 'file://' when using the --template-body flag.
AWS CLI expects the template file path to be prefixed with 'file://' to read it correctly.
Always use 'file://' before the local file path, like 'file://template.yaml'.
Trying to create a stack with a name that already exists without updating it.
Stack names must be unique; creating a stack with an existing name causes an error.
Use a new stack name or update the existing stack with 'aws cloudformation update-stack'.
Summary
CloudFormation templates use JSON or YAML files to describe AWS resources.
Validate templates with 'aws cloudformation validate-template' before creating stacks.
Create stacks with 'aws cloudformation create-stack' and check status with 'describe-stacks'.