LoginSignup
0
0

AWS Console-to-CodeでCloudFormationTemplateを作成する

Last updated at Posted at 2023-12-03

はじめに

AWS Updateにて、AWS Console-to-Code(Preview)というサービスが発表されました!

このサービスはEC2コンソールの操作を記録し、行った操作を生成AIでコード化できるサービスです。
(最初すべてのサービスに対応していると勘違いしていたのですが、現状はEC2コンソール上の操作のみのようです。)

これにより、コンソール上でポチポチ設定したリソースをコード化することが容易になりました。

試してみた

本サービスを利用して、EC2インスタンスとセキュリティグループを作成するCloudFormation Templateを生成してみます。

サービスには「EC2」→「Console-to-Code」からアクセスすることができます。
image01.png

「記録したアクション」欄に今までEC2コンソール上で操作してきたアクションが一覧として表示されます。
タイプでソートすることができ、「差異を表示」とすることで起動や停止といった実際にリソースに影響を与えたアクションを確認することができます。

試しにEC2インスタンスの立ち上げのアクションとセキュリティグループの作成のアクションを選択し、CloudFormation Templateを作成します。
RunInstancesCreateSecurityGroupオペレーションを選択し、YMALコードの生成を選択します。

image02.png

以下のように出力されました。

image03.png

出力結果

Here is the generated YAML CloudFormation code:

Resources:

  EC2Instance: 
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0230bd60aa48260c6
      InstanceType: t2.micro
      NetworkInterfaces: 
        - AssociatePublicIpAddress: true
          DeviceIndex: 0
          GroupSet: 
            - sg-09bbfd95331296085
          SubnetId: subnet-0b8d696172a6bd25e
      TagSpecifications:
        - ResourceType: instance  
          Tags:
            - Key: Name
              Value: test-instance
      MetadataOptions:
        HttpTokens: required
        HttpEndpoint: enabled  
        HttpPutResponseHopLimit: 2
      PrivateDnsNameOptions:
        HostnameType: ip-name
        EnableResourceNameDnsARecord: false
        EnableResourceNameDnsAAAARecord: false
      ClientToken: 62369ec1-5792-48ca-a699-d465573d0f2d
      Count: 1

  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: launch-wizard-1
      GroupDescription: launch-wizard-1 created 2023-12-01T06:43:46.239Z
      VpcId: vpc-02cdf781b703809ac
      

Reasoning:

  • Created an EC2 instance resource with the provided AMI, instance type, network interface configuration, tags, metadata options, private DNS options, client token and count.
  • Created a security group resource with the provided name, description and VPC ID.
  • Used CloudFormation resource types and properties to match the CLI commands.
  • Followed CloudFormation best practices for formatting and structure.

これをこのまま利用してCloudFormation Templateとしてスタックを立てようとしたのですが、エラーが発生しました。
Encountered unsupported property TagSpecificationsということで、一部サポートされていないプロパティを利用していました。

AWS::EC2::Instanceのプロパティをドキュメントから確認し、以下のように手直しすることでスタックの作成ができました。

修正後
Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0230bd60aa48260c6
      InstanceType: t2.micro
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          DeviceIndex: 0
          GroupSet:
            - !Ref SecurityGroup
          SubnetId: subnet-0b8d696172a6bd25e
      Tags:
        - Key: Name
          Value: test-instance

  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: launch-wizard-1
      GroupDescription: launch-wizard-1 created 2023-12-01T06:43:46.239Z
      VpcId: vpc-02cdf781b703809ac

さいごに

AWS Console-to-Codeを利用してCloudFormation Templateを生成してみました。
コンソール上で行った操作をテンプレート化できる機能は非常に良いのですが、作成したテンプレートをそのままスタックの作成に利用できなかった点が気になりました。
現状はPreview段階なので、今後Updateされることでより使いやすいサービスになるのではと思います。

0
0
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
0
0