Introduction
AWS CloudFormation is an Amazon service that allows you to manage your infrastructure as code. It lets you describe and provision your AWS resources in a repeatable and automated way, handling dependencies between those resources as well. With CloudFormation, you write templates in JSON or YAML format, which represent the resources you want to create or update in AWS. You can then launch and manage those resources as a single stack, with CloudFormation taking care of the necessary details.
In this tutorial, you’ll learn how to use CloudFormation to manage your infrastructure in AWS. We’ll go through the basics of CloudFormation and show you how to create, update, and delete a simple stack. You’ll also learn some advanced concepts involving CloudFormation, such as creating and using custom resources in your templates.
Prerequisites
Before starting with the tutorial, make sure you have the following prerequisites:
- AWS account with appropriate permissions to use CloudFormation
- Basic understanding of AWS services, such as EC2, VPC, and IAM.
- Basic knowledge of YAML or JSON, as AWS CloudFormation templates are written in these languages.
Setting up AWS Credentials
In this tutorial, we’ll use the AWS CLI to upload and manage our CloudFormation templates. Before we can use the AWS CLI, we need to configure our AWS credentials.
- Open a terminal or command prompt.
- Install the AWS CLI by executing the following command:
pip install awscli
- After installation, execute the following command to configure your AWS credentials:
aws configure
- You’ll be prompted for your AWS Access Key ID, AWS Secret Access Key, Region name, and output format. Enter the appropriate values for your AWS account.
Once the credentials are configured, we’re ready to proceed with the tutorial.
Creating Your First CloudFormation Stack
In this section, we’ll create a simple CloudFormation stack that creates an EC2 instance in AWS. The stack will use a CloudFormation template written in YAML format.
- Open a new file called
simple-stack.yaml
, and copy the following code into it:
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0c94855ba95c71c99
InstanceType: t2.micro
KeyName: my-key-pair
Tags:
- Key: Name
Value: EC2Instance
- In this template, we’re creating an EC2 instance with a specific AMI (Amazon Machine Image), instance type, key pair, and a tag with a name of “EC2Instance”. Note that the
my-key-pair
value forKeyName
should match a key pair that already exists in your AWS account. - Save the
simple-stack.yaml
file.
Now that the CloudFormation template is ready, we can deploy it to AWS.
- Open a terminal or command prompt.
- Use the AWS CLI to create the CloudFormation stack by executing the following command:
aws cloudformation create-stack --stack-name=MySimpleStack --template-body=file://simple-stack.yaml
- This command will create a CloudFormation stack called
MySimpleStack
using thesimple-stack.yaml
template we just created. Wait for the stack creation to complete.
You can also create the stack from the AWS Management Console by selecting CloudFormation, then click Create stack. Choose a template, select the option “Upload a template to Amazon S3” and navigate to the CloudFormation file you created (in YAML or JSON). Make sure you select a name and tags for the stack and choose the parameters needed. You can always come back later and edit those options.
Once the stack is created, you can view the EC2 instance in your AWS account by navigating to the EC2 service in the AWS Management Console. You should see an EC2 instance with the name “EC2Instance”.
Updating Your CloudFormation Stack
In this section, we’ll make a change to the CloudFormation stack we just created, and update it with the new changes.
- Open the
simple-stack.yaml
file you created earlier. - Change the
InstanceType
property of theEC2Instance
resource to a larger instance size, such asm5.large
.
Resources:
EC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0c94855ba95c71c99
InstanceType: m5.large
KeyName: my-key-pair
Tags:
- Key: Name
Value: EC2Instance
- Save the updated template to disk.
Now, we’ll update the CloudFormation stack with the changes we just made.
- Open a terminal or command prompt.
- Use the AWS CLI to update the CloudFormation stack by executing the following command:
aws cloudformation update-stack --stack-name=MySimpleStack --template-body=file://simple-stack.yaml
- This command will update the
MySimpleStack
stack with the new changes we made to thesimple-stack.yaml
template. Wait for the stack update to complete.
During the update, CloudFormation will create a new instance with the updated instance type and wait for it to become available. It will then terminate the old instance, and update the stack to use the new instance instead.
Deleting Your CloudFormation Stack
In this section, we’ll delete the CloudFormation stack we created in the previous sections.
- Open a terminal or command prompt.
- Use the AWS CLI to delete the CloudFormation stack by executing the following command:
aws cloudformation delete-stack --stack-name=MySimpleStack
- This command will delete the
MySimpleStack
stack. Wait for the stack to be deleted completely.
Alternatively, you can delete the stack from the AWS Management Console by selecting the stack and clicking on “Delete”. Note that deleting a stack will also delete all its resources. This is why don’t ever delete a stack by mistake.
Advanced Concepts: Creating and Using Custom Resources
In this section, we’ll cover how to create and use custom resources in CloudFormation. Custom resources allow you to extend CloudFormation with your own resources or custom logic, making it more flexible and powerful.
To create custom resources, you can use AWS Lambda functions. A Lambda function can be invoked from CloudFormation, and it can create or update resources in AWS based on the input data from CloudFormation.
In this example, we’ll create a custom resource that returns a random quote whenever it’s invoked.
- Create a new file called
random-quote.yaml
, and copy the following code into it:
Resources:
RandomQuote:
Type: Custom::RandomQuote
Properties:
ServiceToken: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:random-quote'
- In this CloudFormation template, we’re creating a custom resource of type
Custom::RandomQuote
. TheProperties
section specifies the Service Token of the Lambda function that will handle this custom resource. Note that the arn value must match the ARN for your Lambda function. - Save the
random-quote.yaml
file.
Now, we’ll create the Lambda function that will provide the random quotes.
- Open a new file called
random-quote.js
, and copy the following code into it:
exports.handler = async (event) => {
const quotes = [
"The best way to predict the future is to invent it.",
"There is no such thing as a free lunch.",
"Winter is coming."
];
const quote = quotes[Math.floor(Math.random() * quotes.length)];
return {
"quote": quote
};
};
- This Node.js function returns a random quote from a list each time it’s invoked.
- Save the
random-quote.js
file.
Next, we’ll package and deploy the Lambda function to AWS.
- Create a Lambda function deployment package by executing the following command in your terminal or command prompt:
zip -r random-quote.zip random-quote.js
- This command creates a ZIP file (
random-quote.zip
) that includes the Node.js function we just created. - Use the AWS CLI to create a new Lambda function from the
random-quote.zip
package. Execute the following command:
aws lambda create-function --function-name=random-quote --runtime=nodejs12.x --handler=random-quote.handler --role=<your-lambda-role-arn> --zip-file=fileb://random-quote.zip
- This command will create a new AWS Lambda function called
random-quote
using the Node.jsrandom-quote.js
file as the function code. Replace<your-lambda-role-arn>
with the Amazon Resource Name (ARN) of an IAM role that your Lambda function can assume. This role should have permissions to write logs to Amazon CloudWatch Logs. - Once the deployment is finished, verify that the function was created by navigating to the Lambda service in the AWS Management Console.
We’re now ready to deploy the random-quote.yaml
CloudFormation template that uses the Custom::RandomQuote
type.
- Use the AWS CLI to create the CloudFormation stack by executing the following command:
aws cloudformation create-stack --stack-name=MyRandomQuoteStack --template-body=file://random-quote.yaml
- Wait for the stack creation to complete, and check that the
RandomQuote
resource was successfully created.
To test the RandomQuote
resource, we’ll use the AWS CLI to invoke it and return a random quote.
- Execute the following command to invoke the
RandomQuote
resource:
aws cloudformation describe-stacks --stack-name=MyRandomQuoteStack --query 'Stacks[].Outputs[?OutputKey==`Quote`].OutputValue' --output text
- If the command is successful, you should see a random quote printed to the console.
Conclusion
In this tutorial, we went through the basics of AWS CloudFormation, from creating, updating, and deleting a stack, to more advanced topics such as custom resources. We hope this tutorial has given you a solid understanding of how to manage your infrastructure as code in AWS, and how CloudFormation can help you streamline your workflow and make your infrastructure more resilient, scalable, and cost-effective.
By using CloudFormation, you can automate the deployment and management of your AWS resources in a repeatable and consistent way, allowing you to focus on developing and innovating your applications rather than managing infrastructure.