Guided Lab: Creating an Amazon VPC with AWS Console-to-Code
Description
AWS Console-to-Code is a feature that allows AWS users to transform actions performed in the AWS Management Console into reusable code like CLI commands. Console-to-Code can also generate IaC templates in several languages and formats, including CDK Java, CDK Python, CDK TypeScript, CloudFormation JSON, and CloudFormation YAML. This feature is useful for creating Infrastructure as Code (IaC) scripts based on manual configurations. By capturing the console actions as code, you can quickly replicate infrastructure setups, automate deployments, and maintain consistency across environments.
In this lab, we’ll use Console-to-Code to record the creation of a VPC, modify the generated code, and then use the AWS CLI to automate VPC creation.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of Amazon VPC and AWS CLI.
If you find any gaps in your knowledge, consider taking the following lab:
Objectives
By the end of this lab, you will:
- Learn how to use Console-to-Code to capture VPC creation steps.
- Understand how to edit and reuse the generated code.
- Set up and configure an AWS CLI environment to automate VPC creation.
Lab Steps
Record VPC Creation Using Console-to-Code
1. Navigate to VPC Console.
2. Open the Console-to-Code feature and Start recording. Then, click Create VPC.
3. Configure the following VPC settings:
- VPC Name: MyCustomVPC1111
- IPv4 CIDR block: 10.0.0.0/16
- Leave other settings as default.
- Click Create VPC to complete the setup.
4. Stop the recording in Console-to-Code to generate code based on your actions.
Copy and Edit the Generated Code
1. In the Console-to-Code output, view the captured AWS CLI commands.
- Select the createVpc, and click on Copy CLI.
2. Copy the code and paste it into a text editor.
- Edit the code to create a new VPC:
- Change the VPC name from
MyCustomVPC1111
toMyCustomVPC2222
. - Change the CIDR block also. ( For example, 172.0.0.0/16)
- Change the VPC name from
3. Save the modified code for later use.
Create AWS Access Keys
1. Go to IAM in the AWS Console.
2. Go to Users > Click on your current User name > Security credentials > Create access key.
3. Follow the following configurations:
- Use case: Select Command Line Interface (CLI)
- Check on the Confirmation
- Click on Next
- Description tag value: myCLIAccessKey
- Click on Create access key
4. After creating the access key, copy the Access key and Secret access key to your clipboard. You can also download the .csv file of it.
Run the Code to Create the VPC
1. Open a terminal on your local machine and configure the AWS CLI:
aws configure
- Enter your Access Key ID, Secret Access Key, preferred AWS region, and output format when prompted.
2. Copy and paste the modified code to create the new VPC:
3. Go to the VPC Console and verify that both MyCustomVPC1111
and MyCustomVPC2222
are present.
- Confirm that the configurations (CIDR block, region) match the specifications in the code.
Congratulations! You have successfully automated the creation of a VPC using Console-to-Code. By capturing manual steps and converting them to code, you could create and automate infrastructure setup, demonstrating how Console-to-Code simplifies the adoption of Infrastructure as Code (IaC).
Key Takeaways:
- Console-to-Code captures console actions as reusable code, aiding the transition to Infrastructure as Code.
- The AWS CLI for infrastructure automation reduces manual setup time and ensures consistency.
- Modifying generated code enables quick replication and customization of resources.
As a best practice, remember to delete any resources no longer in use to maintain a clutter-free AWS environment.
Thank you for joining this lab, and happy learning!