Guided Lab: Creating an Amazon EC2 Instance 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 an EC2 instance, then modify the code to create a second instance with minimal changes.
Prerequisites
This lab assumes you have the following:
- Basic knowledge and understanding of Amazon EC2 and basic Coding.
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 EC2 creation steps.
- Understand how to edit and reuse the code for additional resources.
- Set up and configure an AWS CLI environment to automate EC2 instance creation.
Lab Steps
Record EC2 Instance Creation Using Console-to-Code
1. Navigate to the EC2 Dashboard.
2. Open the Console-to-Code feature.
3. Start recording the creation of a new EC2 instance.
4. Click Launch Instance and configure the following:
- Name:
MyWebServer1111
- AMI: Choose Amazon Linux
- Instance Type: Select
t2.micro
(eligible for free tier) - Key Pair: Select Proceed without a keypair
- Click Launch Instance
5. Stop recording in Console-to-Code.
Copy and Edit the Generated Code
1. In the Console-to-Code output, view the captured AWS CLI commands.
- Select the RunInstances, and click on Copy CLI.
2. Paste the code into a text editor and modify it as follows:
- Change the instance name from
MyWebServer1111
toMyWebServer2222
. You could also create how many instances you want by changing the value of –count.
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 EC2 Instance
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.
2. Copy and paste the modified code to create the EC2 instance with the modified name. Wait for the CLI to confirm that the instance MyWebServer2222
has been successfully created.
3. Go to the EC2 Console and check the list of running instances.
- Verify that both
MyWebServer1111
andMyWebServer2222
are present. - Confirm that the configuration (instance type, AMI, security group) matches the specifications used in the code.
Congratulations! You have successfully used Console-to-Code to record and automate EC2 instance creation. You created a new EC2 instance through the AWS CLI by capturing console actions and modifying the generated code. This lab demonstrates the power of Console-to-Code for converting manual steps into reusable scripts, enabling faster and more consistent infrastructure provisioning.
Key Takeaways:
- Console-to-Code enables users to capture reusable console actions, facilitating the transition to Infrastructure as Code.
- Automating infrastructure with AWS CLI reduces manual effort and improves repeatability.
- Editing captured code allows you to modify and deploy similar resources quickly.
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!