1
1

AWS Console-to-Code 新機能を利用しました。

Last updated at Posted at 2023-11-28

2023年11/26 AWS はAWS Console-to-Codeがプレビュー中であることを発表しました。AWS Console-to-Codeは、コンソールで実行されたアクションを選択した言語で再利用可能なコードに変換することができています。AWS Console-to-Codeは、アクションとワークフローを自動的にキャプチャし、わずかなクリックでそれに対するコードを生成します。ユーザーはこれらのコードスニペットをCloudFormation、CDKなど、希望する形式で生成できます。

Console-to-Codeの利用について

現在、AWS Console-to-Codeはバージニア北部(us-east-1)のEC2コンソールのみで提供されています。

以下の形式であれば、Console-to-Codeを使用してコード変換が可能なようです。

・CDK
Java
Python
TypeScript

・CloudFormation
JSON
YAML

※2023年11月時点ではPreview版

実際利用手順

STEP1:

us-east-1(バージニア北部)リージョンのEC2コンソールに移動してください。
image.png

STEP:2

事前にAWSのマネコンでEC2インスタンスを作成し、Code-to-Consoleを利用してこの操作を記録してコードを生成します。左上の「Console-to-Code」という機能が追加されているのでクリックします。
image.png

STEP:3

以下の画面に遷移されます。
これはEC2のマネコン上で行った操作が記録されています。

※タイプ
「変異を表示」:作成/変更/削除操作のみ表示されます
「読み取り表示」:取得操作のみ表示されます。

image.png

STEP:4

今回は「オペレーション」がRunInstancesで記録されているEC2インスタンスの作成操作をコードに変換します。
image.png

まず:CDK(Python)に変換します。右のオレンジ色のラジオボタンをクリックして、Pythonコードの生成をクリックします。

Here is the generated CDK Python code:

from aws_cdk import (
    aws_ec2 as ec2,
    aws_iam as iam,
    core
)

app = core.App()
stack = core.Stack(app, "testStack")

# Create EC2 instance
instance = ec2.Instance(
    stack, "Instance",
    instance_type=ec2.InstanceType("t2.micro"),
    machine_image=ec2.AmazonLinuxImage(generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2), 
    key_name="test",
    vpc=ec2.Vpc.from_lookup(stack, "VPC", vpc_id="vpc-1234abcd"), #vpc_id to be replaced
    subnet=ec2.Subnet.from_subnet_id(stack, "Subnet", subnet_id="subnet-0f347df38213717d7"),
    block_devices=[ec2.BlockDevice(device_name="/dev/xvda", volume=ec2.BlockDeviceVolume.ebs(30))],
    security_group=ec2.SecurityGroup.from_security_group_id(stack, "SG", security_group_id="sg-0338213988c51bc9c"),
    instance_name="testConsole-to-code"
)

instance.node.add_metadata("HttpTokens", "required") 
instance.node.add_metadata("HttpEndpoint", "enabled")
instance.node.add_metadata("HttpPutResponseHopLimit", 2)

core.CfnOutput(stack, "InstanceId", value=instance.instance_id)

補足事項も提供されます

Reasoning:
- Imported the necessary CDK modules
- Created a CDK App and Stack
- Looked up the VPC and Subnet using their ids instead of creating new ones
- Used the provided AMI id, instance type, key name and security group
- Added the instance name tag
- Added the metadata options
- Added an output for the instance id

Let me know if you have any other questions!

次はCloudFormationに変換をためしてみます。YAMLに変換してみます。

Here is the generated YAML CloudFormation template:

Resources:

  EC2Instance: 
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0230bd60aa482688888 
      InstanceType: t2.micro
      KeyName: test
      NetworkInterfaces: 
        - AssociatePublicIpAddress: false
          DeviceIndex: 0
          GroupSet: 
            - sg-0338213988c51bc9c
          SubnetId: subnet-0f347df38213717d7
      TagSpecifications:
        - ResourceType: instance  
          Tags:
            - Key: Name
              Value: testConsole-to-code
      MetadataOptions:
        HttpTokens: required
        HttpEndpoint: enabled  
        HttpPutResponseHopLimit: 2
      PrivateDnsNameOptions:
        HostnameType: ip-name
        EnableResourceNameDnsARecord: false
        EnableResourceNameDnsAAAARecord: false
      ClientToken: 58038a71-ee97-4933-8fa9-06aea6847562
      Count: 1
      

補足事項も提供されます。

Reasoning:
- Created an EC2Instance resource of type AWS::EC2::Instance to match the ec2 run-instances CLI command
- Set ImageId, InstanceType, KeyName properties from corresponding CLI options
- Created a NetworkInterfaces property list from the CLI network-interfaces option
- Added the SubnetId, AssociatePublicIpAddress, DeviceIndex, GroupSet from the CLI network-interfaces options
- Added TagSpecifications property from CLI tag-specifications option
- Added Tags according to CLI tags
- Added MetadataOptions properties from CLI metadata-options options  
- Added PrivateDnsNameOptions properties from CLI private-dns-name-options options
- Added ClientToken and Count properties from CLI options

ちなみに、CLIコマンドも提供されています。

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1