Infrastructure as Code is a powerful concept that helps organizations maintain consistency and manage changes. In this blog post, we’ll explore how to deploy Infrastructure with AWS CLI and CloudFormation.
The “run aws cli command in cloudformation template” is a process that can be used to deploy infrastructure with AWS CLI and CloudFormation.
Infrastructure was furnished with screwdrivers and wires in steamy basements in the dark days of physical servers. These gloomy days began to fade as public cloud solutions began to supplant on-premises infrastructure as the primary method of provisioning and running servers. Learn how to use AWS CLI and CloudFormation to deploy cloud solutions in this blog article.
Karl Eriksson, cofounder of Mocki, a platform that allows you construct mock APIs in minutes, has written this guest article.
If your application runs in a single environment, you may simply administer it using a graphical user interface (GUI) like the AWS Console. However, having various environments like as development, staging, and production makes things more difficult. Time and human error come into play.
You must code your infrastructure management. In the AWS world, CloudFormation stacks are the way to go.
AWS will generate everything from scratch when installing a CloudFormation stack for the first time. When you deploy the stack as an update, it will identify any infrastructure changes and only deploy those changes.
CloudFormation stacks guarantee that the same updates are made in all of your environments.
AWS Infrastructure as Code with CloudFormation
In an AWS context, AWS CloudFormation is a tool for authoring and deploying infrastructure as code. CloudFormation supports the majority of AWS services. Virtual servers, databases, load balancers, and file storage may all be added to a CloudFormation stack created using the AWS Console. All of those resources may then be distributed using a command line or another automated method after they’ve been added.
When it comes to updates, CloudFormation ensures that activities are carried out as efficiently as possible, including rolling back modifications if anything goes wrong.
Let’s look at how to use the AWS CLI and CloudFormation to build infrastructure.
Prerequisites
To follow along with the remainder of this article’s CloudFormation tutorial, make sure you have the following:
- AWS credentials
- Learn how to download and set up the AWS CLI here. By executing aws s3 ls, you can verify that the CLI was correctly configured. All is good if the command returns successfully.
If you have numerous AWS profiles established using the AWS CLI, choose one that is appropriate for development or testing for this lesson.
Getting Started with CloudFormation
With everything out of the way, let’s get down to business and build a CloudFormation stack!
In this example, you’ll build a stack that includes the infrastructure required to publish a static website to AWS S3, which is a static file hosting service.
To begin, create a new template.yaml file and fill it with the following content:
2010-09-09 AWSTemplateFormatVersion: Resources: A basic CloudFormation template Description: Bucket: AWS::S3::Type Bucket Characteristics: BucketName: your-bucket-name # give it a name of your own.
- AWSTemplateFormatVersion – specifies the template’s format version so that AWS can appropriately comprehend it when it’s deployed.
- Description – a description of the template that will appear in the AWS Console after it has been published.
- Resources – this section includes all of the template’s infrastructure. The resources will generate a basic S3 bucket that will host a static site in this scenario.
Attempt to come up with anything other than your-bucket-name for the BucketName attribute.
Using AWS CLI and CloudFormation to deploy the stack
Now that you have a basic template to deploy, use the AWS CLI and CloudFormation to deploy it to AWS using the command below:
—template-file template.yaml —stack-name static-website aws cloudformation deploy
After you’ve ran the command, go to the AWS Console and choose Services CloudFormation. The deployment should now be underway. It should then reach the CREATE COMPLETE state after a few minutes. If so, it’s finished!
When looking at the specifics of your stack, you should be able to identify any issues in the Events section.
You should now notice a fresh new S3 bucket created by going to Services S3.
Creating a Basic Website
Create an index.html file to act as the website after you’ve set up an S3 bucket to store it.
If you don’t have any ideas, you may use the following material in your index.html file:
<html> <body> <h1>Static Site Deployed Using CloudFormation</h1> <p>Hello! This site was deployed using CloudFormation. Go cloud!</p> </body> </html>
Save the index.html file to your S3 bucket and upload it using the AWS Console.
Because the index file will be available to everyone who visits the site, be sure to give it public read access.
Refreshing the Template
The S3 bucket you just created is currently not configured to host a website. Let’s make a difference. The index file is now available for download via the AWS interface, however it is not accessible over the public internet. Change the setup in CloudFormation to allow static website hosting.
To enable it, make the following changes to the template.yaml file. We’ll add a few more attributes to our template below.
2010-09-09 AWSTemplateFormatVersion: Resources: A basic CloudFormation template Description: Bucket: AWS::S3::Type Bucket Characteristics: BucketName: your-bucket-name # give it a name of your own. AccessControl: PublicRead WebsiteConfiguration: IndexDocument: index.html Outputs: WebsiteURL: Value: !GetAtt [Bucket, WebsiteURL] Description: URL for website hosted on S3
- Sets the S3 bucket’s access privileges to public read, allowing any user who visits the site to view the index.html file.
- When visitors browse to the bucket URL, WebsiteConfiguration configures the S3 bucket to function as a website, serving the index.html file submitted.
- Outputs – these may be various characteristics for resources in the stack. The stack output in the example above is the WebsiteURL. The website URL should appear as an output in the AWS CloudFormation Console when you re-deploy the template.
Now, use the same command as before to deploy the modifications to the existing stack:
—template-file template.yaml —stack-name static-website aws cloudformation deploy
Go to the CloudFormation console to validate the outcome. The stack should be at the UPDATE COMPLETE state after a few minutes. Go to the Outputs tab when the stack reaches this stage, and you should see the WebsiteUrl added as an output in the template.
Your site should appear in your browser when you click the URL given.
Congratulations! You’ve just launched your first infrastructure as code stack using the AWS CLI and CloudFormation! Continue to add to your template file to deploy fresh modifications or develop a new one based on your requirements.
Conclusion
You learnt about the notion of infrastructure as code and the benefits it provides over manual infrastructure provisioning in this session. You then deployed an AWS CloudFormation template to construct a static website using the infrastructure as code technique.
You may use CloudFormation to deploy a production-ready system utilizing the ideas covered in this lesson.
Additional Reading
Check out HashiCorp’s Infrastructure as Code in the Real World for additional information on the phenomena of infrastructure as code.
If you want to learn more about integrating CloudFormation into your development flow and utilizing CloudFormation to manage all of your infrastructure, go visit:
Good luck with your road to leveraging AWS to deploy all of your infrastructure as code!
The “aws cli cloudformation example” is a blog post that provides an overview of deploying infrastructure with the AWS CLI and CloudFormation. The blog post covers how to create a new stack, describe it, and deploy it.
Related Tags
- aws cloudformation deploy example
- aws cloudformation deploy –parameter file
- aws cloudformation template
- cloudformation deploy cli
- aws cloudformation cli