AWS Console-to-Code: Transform Console Actions into Reusable Code

NewsAWS Console-to-Code: Transform Console Actions into Reusable Code

AWS has just announced the general availability of a new tool called AWS Console-to-Code. This innovative feature is designed to simplify and streamline the process of converting actions performed in the AWS console into reusable code. With AWS Console-to-Code, users can easily record their interactions and workflows within the AWS console, such as when they’re launching an Amazon Elastic Compute Cloud (Amazon EC2) instance, and then review the corresponding AWS Command Line Interface (AWS CLI) commands for those actions. This functionality is especially useful for users who want to automate their infrastructure setup or incorporate these commands into broader automation pipelines.

The AWS Console-to-Code tool allows users to generate code in various infrastructure-as-code (IaC) formats, including AWS CloudFormation templates (in either YAML or JSON) and AWS Cloud Development Kit (AWS CDK) scripts (in languages such as TypeScript, Python, or Java). This flexibility means that users can choose the format that best fits their needs and existing workflows, whether they are looking to automate infrastructure deployment or integrate these capabilities into their development pipelines.

Since its preview release last year, AWS Console-to-Code has received positive feedback from users. The general availability version has been enhanced further based on customer feedback to make it even more useful and user-friendly.

New Features in General Availability (GA)

  1. Support for More Services: During its preview phase, AWS Console-to-Code was limited to Amazon EC2. However, with the general availability release, it now also supports Amazon Relational Database Service (RDS) and Amazon Virtual Private Cloud (Amazon VPC). This expanded support means that users can now automate a broader range of AWS services.
  2. Simplified User Experience: AWS has worked to make the user experience more intuitive and easier to navigate, which simplifies the processes of prototyping, recording, and generating code.
  3. Preview Code: The launch wizards for EC2 instances and Auto Scaling groups have been updated, allowing users to generate code for these resources without actually having to create them in the AWS environment. This feature is particularly useful for those who want to experiment with infrastructure configurations without incurring the costs associated with resource creation.
  4. Advanced Code Generation: The code generation process for AWS CDK and CloudFormation is driven by machine learning models from Amazon Q. This adds an extra layer of sophistication, ensuring that the generated code is as efficient and accurate as possible.

    Getting Started with AWS Console-to-Code

    To illustrate how AWS Console-to-Code works, let’s look at a simple example of launching an Amazon EC2 instance. First, navigate to the Amazon EC2 console. On the right-hand side, you will find the AWS Console-to-Code widget. Click on "Start recording" to begin tracking your actions.

    Once you have launched an EC2 instance using the instance launch wizard in the Amazon EC2 console, stop the recording to finalize the process. In the "Recorded actions" table, you can review the actions that were recorded. Use the "Type" dropdown menu to filter for write actions and select the "RunInstances" action. From there, you can copy the AWS CLI command that corresponds to your recorded actions.

    Here’s an example of a CLI command generated by AWS Console-to-Code:

    shell<br /> aws ec2 run-instances \<br /> --image-id "ami-066784287e358dad1" \<br /> --instance-type "t2.micro" \<br /> --network-interfaces '{"AssociatePublicIpAddress":true,"DeviceIndex":0,"Groups":["sg-1z1c11zzz1c11zzz1"]}' \<br /> --credit-specification '{"CpuCredits":"standard"}' \<br /> --tag-specifications '{"ResourceType":"instance","Tags":[{"Key":"Name","Value":"c2c-demo"}]}' \<br /> --metadata-options '{"HttpEndpoint":"enabled","HttpPutResponseHopLimit":2,"HttpTokens":"required"}' \<br /> --private-dns-name-options '{"HostnameType":"ip-name","EnableResourceNameDnsARecord":true,"EnableResourceNameDnsAAAARecord":false}' \<br /> --count "1"<br />

    This command can be easily modified. For example, you can adjust it to launch two instances (--count 2) of a different type, such as t3.micro (--instance-type). This is just one example, but the same approach can be applied to other AWS workflows.

    I ran this command using AWS CloudShell, and it worked perfectly, launching two t3.micro EC2 instances. This illustrates how the CLI code generation experience is based on the API commands executed during the console actions.

    Infrastructure as Code (IaC) Generation Using AWS CDK

    AWS CDK is an open-source framework that allows users to define cloud infrastructure in code and provision it through AWS CloudFormation. With AWS Console-to-Code, users can generate AWS CDK code (in Java, Python, and TypeScript) for their infrastructure workflows.

    Continuing with the EC2 instance launch example, if you haven’t done so already, start recording the actions in the Amazon EC2 console, launch an EC2 instance, and stop the recording. Then, choose the "RunInstances" action from the "Recorded actions" table to generate AWS CDK Python code by selecting the "Generate CDK Python" option.

    You can use the generated code as a starting point, tailoring it to meet your specific requirements for production environments. For instance, I had AWS CDK installed and created a new Python CDK project. I modified the code to fit into an AWS CDK Stack, changed the EC2 instance type, and made other minor adjustments to ensure accuracy. I then successfully deployed it using cdk deploy.

    The AWS Console-to-Code tool not only facilitates the creation of AWS CDK scripts but also allows the generation of CloudFormation templates in YAML or JSON format. This capability ensures that you have the flexibility to choose the most suitable format for your infrastructure as code needs.

    Preview Code

    AWS Console-to-Code also offers a "Preview code" feature, which allows users to access the tool directly from the Amazon EC2 and Amazon EC2 Auto Scaling group launch experience. This means you can generate infrastructure code without having to create the actual resource, saving time and resources.

    To try this feature, follow the steps to create an Auto Scaling group using a launch template. Instead of clicking on "Create Auto Scaling group," select "Preview code" to see the options for generating infrastructure code or copying the AWS CLI command.

    Things to Know

    Here are a few key points to keep in mind when using AWS Console-to-Code:

    • AWS Console-to-Code is available to all users for generating AWS CLI commands for their infrastructure workflows. However, the code generation feature for AWS CDK and CloudFormation formats is limited to 25 free generations per month. Beyond that, an Amazon Q Developer subscription is required.
    • It’s strongly recommended that you test and verify the generated infrastructure as code before deployment to ensure it meets your requirements.
    • As of the general availability release, AWS Console-to-Code only records actions taken in the Amazon EC2, Amazon VPC, and Amazon RDS consoles.
    • The "Recorded actions" table in AWS Console-to-Code only displays actions performed during the current session within the specific browser tab. It does not retain actions from previous sessions or other tabs. Refreshing the browser tab will result in the loss of all recorded actions.

      Now Available

      AWS Console-to-Code is now available in all commercial AWS Regions. For more information, you can refer to the Amazon EC2 documentation. Try it out in the Amazon EC2 console and share your feedback through the AWS re:Post for Amazon EC2 or via your usual AWS Support channels.

For more Information, Refer to this article.

Neil S
Neil S
Neil is a highly qualified Technical Writer with an M.Sc(IT) degree and an impressive range of IT and Support certifications including MCSE, CCNA, ACA(Adobe Certified Associates), and PG Dip (IT). With over 10 years of hands-on experience as an IT support engineer across Windows, Mac, iOS, and Linux Server platforms, Neil possesses the expertise to create comprehensive and user-friendly documentation that simplifies complex technical concepts for a wide audience.
Watch & Subscribe Our YouTube Channel
YouTube Subscribe Button

Latest From Hawkdive

You May like these Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.