{"id":3943,"date":"2023-11-04T23:13:57","date_gmt":"2023-11-04T23:13:57","guid":{"rendered":"http:\/\/localhost:10003\/managing-your-infrastructure-using-aws-cloudformation\/"},"modified":"2023-11-05T05:48:26","modified_gmt":"2023-11-05T05:48:26","slug":"managing-your-infrastructure-using-aws-cloudformation","status":"publish","type":"post","link":"http:\/\/localhost:10003\/managing-your-infrastructure-using-aws-cloudformation\/","title":{"rendered":"Managing your infrastructure using AWS CloudFormation"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
Before starting with the tutorial, make sure you have the following prerequisites:<\/p>\n
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.<\/p>\n
pip install awscli\n<\/code><\/pre>\n\n- After installation, execute the following command to configure your AWS credentials:<\/li>\n<\/ol>\n
aws configure\n<\/code><\/pre>\n\n- 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.<\/li>\n<\/ol>\n
Once the credentials are configured, we’re ready to proceed with the tutorial.<\/p>\n
Creating Your First CloudFormation Stack<\/h1>\n
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.<\/p>\n
\n- Open a new file called
simple-stack.yaml<\/code>, and copy the following code into it:<\/li>\n<\/ol>\nResources:\n EC2Instance:\n Type: AWS::EC2::Instance\n Properties:\n ImageId: ami-0c94855ba95c71c99\n InstanceType: t2.micro\n KeyName: my-key-pair\n Tags:\n - Key: Name\n Value: EC2Instance\n<\/code><\/pre>\n\n- 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<\/code> value for KeyName<\/code> should match a key pair that already exists in your AWS account.<\/li>\n- Save the
simple-stack.yaml<\/code> file.<\/li>\n<\/ol>\nNow that the CloudFormation template is ready, we can deploy it to AWS.<\/p>\n
\n- Open a terminal or command prompt.<\/li>\n
- Use the AWS CLI to create the CloudFormation stack by executing the following command:<\/li>\n<\/ol>\n
aws cloudformation create-stack --stack-name=MySimpleStack --template-body=file:\/\/simple-stack.yaml\n<\/code><\/pre>\n\n- This command will create a CloudFormation stack called
MySimpleStack<\/code> using the simple-stack.yaml<\/code> template we just created. Wait for the stack creation to complete.<\/li>\n<\/ol>\nYou 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.<\/p>\n
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”.<\/p>\n
Updating Your CloudFormation Stack<\/h1>\n
In this section, we’ll make a change to the CloudFormation stack we just created, and update it with the new changes.<\/p>\n
\n- Open the
simple-stack.yaml<\/code> file you created earlier.<\/li>\n- Change the
InstanceType<\/code> property of the EC2Instance<\/code> resource to a larger instance size, such as m5.large<\/code>.<\/li>\n<\/ol>\nResources:\n EC2Instance:\n Type: AWS::EC2::Instance\n Properties:\n ImageId: ami-0c94855ba95c71c99\n InstanceType: m5.large\n KeyName: my-key-pair\n Tags:\n - Key: Name\n Value: EC2Instance\n<\/code><\/pre>\n\n- Save the updated template to disk.<\/li>\n<\/ol>\n
Now, we’ll update the CloudFormation stack with the changes we just made.<\/p>\n
\n- Open a terminal or command prompt.<\/li>\n
- Use the AWS CLI to update the CloudFormation stack by executing the following command:<\/li>\n<\/ol>\n
aws cloudformation update-stack --stack-name=MySimpleStack --template-body=file:\/\/simple-stack.yaml\n<\/code><\/pre>\n\n- This command will update the
MySimpleStack<\/code> stack with the new changes we made to the simple-stack.yaml<\/code> template. Wait for the stack update to complete.<\/li>\n<\/ol>\nDuring 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.<\/p>\n
Deleting Your CloudFormation Stack<\/h1>\n
In this section, we’ll delete the CloudFormation stack we created in the previous sections.<\/p>\n
\n- Open a terminal or command prompt.<\/li>\n
- Use the AWS CLI to delete the CloudFormation stack by executing the following command:<\/li>\n<\/ol>\n
aws cloudformation delete-stack --stack-name=MySimpleStack\n<\/code><\/pre>\n\n- This command will delete the
MySimpleStack<\/code> stack. Wait for the stack to be deleted completely.<\/li>\n<\/ol>\nAlternatively, 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.<\/p>\n
Advanced Concepts: Creating and Using Custom Resources<\/h1>\n
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.<\/p>\n
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.<\/p>\n
In this example, we’ll create a custom resource that returns a random quote whenever it’s invoked.<\/p>\n
\n- Create a new file called
random-quote.yaml<\/code>, and copy the following code into it:<\/li>\n<\/ol>\nResources:\n RandomQuote:\n Type: Custom::RandomQuote\n Properties:\n ServiceToken: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:random-quote'\n<\/code><\/pre>\n\n- In this CloudFormation template, we’re creating a custom resource of type
Custom::RandomQuote<\/code>. The Properties<\/code> 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.<\/li>\n- Save the
random-quote.yaml<\/code> file.<\/li>\n<\/ol>\nNow, we’ll create the Lambda function that will provide the random quotes.<\/p>\n
\n- Open a new file called
random-quote.js<\/code>, and copy the following code into it:<\/li>\n<\/ol>\nexports.handler = async (event) => {\n const quotes = [\n \"The best way to predict the future is to invent it.\",\n \"There is no such thing as a free lunch.\",\n \"Winter is coming.\"\n ];\n const quote = quotes[Math.floor(Math.random() * quotes.length)];\n\n return {\n \"quote\": quote\n };\n};\n<\/code><\/pre>\n\n- This Node.js function returns a random quote from a list each time it’s invoked.<\/li>\n
- Save the
random-quote.js<\/code> file.<\/li>\n<\/ol>\nNext, we’ll package and deploy the Lambda function to AWS.<\/p>\n
\n- Create a Lambda function deployment package by executing the following command in your terminal or command prompt:<\/li>\n<\/ol>\n
zip -r random-quote.zip random-quote.js\n<\/code><\/pre>\n\n- This command creates a ZIP file (
random-quote.zip<\/code>) that includes the Node.js function we just created.<\/li>\n- Use the AWS CLI to create a new Lambda function from the
random-quote.zip<\/code> package. Execute the following command:<\/li>\n<\/ol>\naws 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\n<\/code><\/pre>\n\n- This command will create a new AWS Lambda function called
random-quote<\/code> using the Node.js random-quote.js<\/code> file as the function code. Replace <your-lambda-role-arn><\/code> 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.<\/li>\n- Once the deployment is finished, verify that the function was created by navigating to the Lambda service in the AWS Management Console.<\/li>\n<\/ol>\n
We’re now ready to deploy the random-quote.yaml<\/code> CloudFormation template that uses the Custom::RandomQuote<\/code> type.<\/p>\n\n- Use the AWS CLI to create the CloudFormation stack by executing the following command:<\/li>\n<\/ol>\n
aws cloudformation create-stack --stack-name=MyRandomQuoteStack --template-body=file:\/\/random-quote.yaml\n<\/code><\/pre>\n\n- Wait for the stack creation to complete, and check that the
RandomQuote<\/code> resource was successfully created.<\/li>\n<\/ol>\nTo test the RandomQuote<\/code> resource, we’ll use the AWS CLI to invoke it and return a random quote.<\/p>\n\n- Execute the following command to invoke the
RandomQuote<\/code> resource:<\/li>\n<\/ol>\naws cloudformation describe-stacks --stack-name=MyRandomQuoteStack --query 'Stacks[].Outputs[?OutputKey==`Quote`].OutputValue' --output text\n<\/code><\/pre>\n\n- If the command is successful, you should see a random quote printed to the console.<\/li>\n<\/ol>\n
Conclusion<\/h1>\n
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.<\/p>\n
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.<\/p>\n","protected":false},"excerpt":{"rendered":"
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 Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[317,478,476,477,474,473,479,475,472],"yoast_head":"\nManaging your infrastructure using AWS CloudFormation - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n