Infrastructure as Code from Existing Resources

Joris Verbogt
Joris Verbogt
Feb 9 2024
Posted in Engineering & Technology

Create templates with AWS IaC Generator

Infrastructure as Code from Existing Resources

Infrastructure as Code

Over the last decade or so, Infrastructure as Code (IaC) has emerged as a cornerstone of modern cloud architecture, enabling developers and system administrators to manage and provision infrastructure resources programmatically. By defining infrastructure in code, IaC tools facilitate automation, version control, and repeatability, thereby reducing manual errors and streamlining deployment processes.

AWS CloudFormation

AWS provides several tools and services to facilitate IaC, most important of which is AWS CloudFormation, which lets you define your infrastructure configuration by using templates in YAML or JSON.

But what if you already have an existing setup that you want to describe in code? Wouldn't it be nice to just have those resources included in a CloudFormation template?

Luckily, AWS recently released the IaC Generator, which allows you to do just that.

IaC Generator

AWS IaC Generator is a powerful tool designed to simplify the process of creating AWS CloudFormation templates. It offers a guided experience for generating infrastructure templates based on best practices and AWS architectural patterns. With AWS IaC Generator, you can define your infrastructure requirements through a series of questions and preferences, allowing the tool to generate a CloudFormation template tailored to your needs.

In this blog post, we'll explore how to leverage the AWS IaC Generator to create templates for an example Virtual Private Cloud (VPC).

Scan your resources

In the CloudFormation service in the AWS Console, choose IaC Generator from the menu.

First step is to scan your existing resources:

iac generator scan

Start a new template

This list of resources can be used to generate one or more templates. Let's create one for our existing VPC resources:

iac generator create template

Find resources to include

Let's select the resources we want to include. They can be searched by Resource Type or Resource Tag. Let's search for VPCs and select our VPC:

iac generator find resources

Next, we can include related resources. In this example, we will include Security Groups, Subnets, Routes and Gateways:

iac generator add resources

Generate the template

Now it's time to generate the actual CloudFormation template. In JSON format, it will look something like this (taking one of the Security Groups as an example):

{
    "Resources": {
        "EC2SecurityGroup00sg059609955c37e6b850029MeJ": {
            "UpdateReplacePolicy": "Retain",
            "Type": "AWS::EC2::SecurityGroup",
            "DeletionPolicy": "Retain",
            "Properties": {
                "GroupDescription": "Allow for HTTPS connection",
                "GroupName": "HTTPS",
                "VpcId": {
                    "Ref": "EC2VPC00vpc25e2de4d00YbUmk"
                },
                "SecurityGroupIngress": [
                    {
                        "CidrIp": "0.0.0.0/0",
                        "IpProtocol": "tcp",
                        "FromPort": 443,
                        "ToPort": 443
                    }
                ],
                "SecurityGroupEgress": [
                    {
                        "CidrIp": "0.0.0.0/0",
                        "IpProtocol": "-1"
                    },
                    {
                        "CidrIpv6": "::/0",
                        "IpProtocol": "-1"
                    }
                ]
            }
        }
    }
}

The template can now be used to create or update a CloudFormation stack, which can then be deployed and maintained when needed.

The actual deployment of CloudFormation stacks is beyond the scope of this blog post, more information can be found in the AWS CloudFormation Documentation.

Conclusion

AWS IaC Generator offers a user-friendly approach to creating CloudFormation templates, allowing you to provision infrastructure resources efficiently and reliably, while adhering to AWS best practices.

As always, we hope you liked this article, and if you have anything to add, we are available via our Support Channel.

Keep up-to-date with the latest news