Create CloudFormation Template From Existing AWS Infra

In my previous blog post , I have explained How to deploy LAMP Stack on EC2 Instance using Cloudformation Template.

In this guide , We will see How to create CloudFormation template from the Existing AWS Infra.

What Is AWS CloudFormation?

AWS CloudFormation is a service which helps us to setup AWS resources such as EC2 , RDS instances in a very less time So that we can focus more on applications.

We have to just create a template with the details of AWS resources such as EC2 , RDS , SNS etc .that needs to be launched to setup applications.

Once the template is created , We can import it to Cloudformation and AWS CloudFormation will take care of provisioning those resources , Configure them and map them if required.

AWS CloudFormation helps us to,

  • Quickly replicate the exiting Infrastructure.
  • Simplify infrastructure management.
  • Easily control and track changes to the infrastructure.

Core Concepts Of CloudFormation

In CloudFormation , We mostly work with these components.

  • Templates

Templates in cloudformation are written in JSON or YAML format.In this template , You can describe the resources with their properties that needs to be created.For example : EC2 Instance with t2.medium ,Then using this template the cloudformation will create those resources.

  • Stacks

A stack is a collection of AWS resources which we can manage as a single unit.All the resources in a stack are defined by cloudformation template.

  • Change Sets

If any changes required in the existing infrastructure , We can update the stack.Before making changes we can create a change set (a summary of proposed changes) and the change set will let us know how the changes will impact the existing resources.

Bringing Existing Resources Into CloudFormation

If the AWS resources are created without the use of CloudFormation , We can still bring the existing resources into AWS CloudFormation management using resource import.

During Import operation we need to provide the following.

  • Identifiers for the resources to import – Two values should be provided to ideatify each target resource.

a)Identifiy Value : The actual value of the target resource. For example: BucketName (property) = TestBucket (actual value)

b)Identifier property : Resource property that is used to identify each resource. For example : AWS::S3::Bucket can be identified using BucketName

  • Template describing the entire stack which includes original stack and the resources to import.

Each resource to import must have DeletionPolicy attribute.

Resources That Supports Import Operations

Before creating the template for the Existing AWS resource , Don’t forget to check the resources that support Import Operations.

API gateway , Athena , App Config , Auto Scaling , AWS Billing and Cost Management, ACM , Cloudtrail , Cloudwatch , AWS Chatbot , CloudFormation , CodeGuru profiler , Detective , AWS Config , AWS CodeStar , DynamoDB , Amazon EC2 , EC2 Image Builder , ECS , EFS , Elastic Load Balancing , Firewall Manager , EventBridge , Global Accelerator , Ground Station , IAM , IAM Access Analyzer , AWS ioT , Lambda , Kinesis , Macie , Network Manager , managed apache cssandra Service , RDS , Resource Groups , QLDB , S3 , Route 53 , SES , SSM , SQS , SNS , AWS Service Catalog , AWS WAF .

Creating Stack From Existing AWS Resources

To create cloudFormation template (Stack) from the existing AWS resources , Login to CloudFormation console.

Click Create stack

On the Create stack page , Under Prerequisite – Prepare template , Choose use a sample template

For Select a sample template , From the drop down , Choose CloudFormer

AWS CloudFormer is a tool used to create CloudFormation template from the existing AWS resources.

We can explicitly choose the services for which the template should be created by the CloudFormer and then the template can be uploaded into the S3 bucket.

and then Click Next

Enter the Stack name.

Under Parameters , We need to Enter credentials for the CloudFormer , This is because , If we launch the CloudFormer It asks us Username and Password to proceed further with the template creation.

Choose the VPC whether the CloudFormer should be created . Basically The stack will create an EC2 instance which will be usedto run CloudFormer.

Choose the Default VPC and then Click Next

You can optionally provide tags for the Stack and then Click Next.

Acknowledge And finally Click Create stack

The Stack creation should be initialized.

In the Events section We can track all the resources that will be created by the Stack.

The Following resources will be created.

InstanceSecurityGroup , EC2Instance , IAMPolicy , IAMRole , IAMInstanceProfile.

In the Resources Section , We can find all the resources created.

In the Outputs Section , We can find the URL of the CloudFormer

Clicking the Link will ask for Username and Password.Provide the credentials and click OK

You should see the following page.

Choose the AWS Region where AWS services are running , from which you want to create the template.

Then , Click Create Template

It will take some time for Analyzing the AWS account

Provide the description for the template which we’re going to create. and Click Continue

Going forward , The following details will be asked.

If we select a Service , The CloudFormer will take care of its dependencies.

  • DNS (Hosted Zones in Route 53 along with DNS records)
  • VPC (Choose the Virtual Private Cloud)
  • VPC NETWORKS (Subnets , Internet Gateway , NAT Gateway , VPN Gateways , DHCP Options , Customer Gateway , peering Connections , VPN Connections)
  • VPC SECURITY (Route Tables , Network ACL’s)
  • NETWORK (Load balancers , Elastic IPs , Cloudfront Distributions)
  • MANAGED SERVICES (AutoScaling groups , Beanstalk Apps)
  • MANAGED CONFIG (ASG launch configurations , Beanstalk Environment and App versions)
  • COMPUTE (EC2)
  • STORAGE (EBS Volumes , RDS , Redshift , S3 , ElastiCache Cluster , Dynamo DB)
  • STORAGE CONFIG (Subnet Groups , Parameter Groups )
  • APP SERVICES (SQS , SNS , Kineses)
  • SECURITY (Security Groups , SNS Topics , Bucket Policies , SQS queue policies)
  • OPERATIONAL (Auto Scaling policies , Cloudwatch Alarms , Cloudtrails , Auto Scaling Scheduled Actions)

After going through all the process , You will get the Summary of all the resources which you have chosen for which the template will be created.

If required , We can modify it.

Finally Click Continue

Provide a Name for the Template and Choose the S3 Bucket whether the template should be uploaded. Click Save Template

At last , You will see the following page.

Using the template , Which we have just created , We can provision the AWS resources by just click Launch Stack

Or Else, Login to Cloudformation Console , Click Create stack , Choose With existing resources (import resources)

Click Next , For template source , Choose Amazon S3 URL (You can find the URL for the cloudformation template from the S3 bucket).

Copy and paste the template Link and click Next , Specify the Stack details and You should be able to recreate the existing Infrastructure.

Terminating Cloudformation Stack

If you want to terminate the whole stack , Go to CloudFormation console.

Choose the stack and then click Delete , On the confirmation page , Click Delete stack

Thanks for reading this article.Hope you found it helpful.

Please do check out my other articles.