🧵

AWS Console-to-Code (Preview) が発表されました

2023/11/27に公開

AWS Console-to-Code (Preview) が発表

2023 年 11 月 26 日 AWS Console-to-Code (Preview) が発表されました。
https://aws.amazon.com/jp/about-aws/whats-new/2023/11/aws-console-to-code-preview-generate-console-actions/

これは、AWS マネジメントコンソールでのプロトタイピングからワークロードの実稼働コードのデプロイまでを、簡単、迅速、かつコスト効率良く行うことができる生成 AI 機能です。お客様は、コンソール アクションのコードを好みの形式で生成できるようになりました。
(中略)
AWS Console-to-Code は、米国東部 (バージニア北部) でプレビューとして利用できます。

との事です。


ドキュメント

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/console-to-code.html

Supported code formats
Console-to-Code can currently generate infrastructure-as-code (IaC) in the following code formats:

  • CDK Java
  • CDK Python
  • CDK TypeScript
  • CloudFormation JSON
  • CloudFormation YAML

早速触ってみる。

ヴァージニアのEC2コンソールに移動するとすぐに左カラムで発見出来ました。現時点での表示です。

🔻
試しにtestという名前でインスタンスを作成してみます。

🔻
無事作成されました。

🔻
Console-to-Code画面に戻り、「タイプ」を「すべてを表示」にすると、アクションが表示されました。LaunchInstance(インスタンス起動)コンソールでのアクションと書いてあるものを選択して「Typscriptコードの生成」をクリックします。(この時点で"CLIコマンド"列にコマンドが存在。)

恐らくインスタンスの作成完了後、CreateInstanceアクションも増えるように思いますが、一旦先に作成されたSecurityGroupだけ選択。なんならDescribeやAuthorizedは選択せず、createだけでよかったかもしれませんがこの時点ではとりあえず3つ選択してみました。

(公式ドキュメントには一度に最大 5 つのアクションを選択可能である旨記載有)

※「▼」をクリックしたリスト

🔻
"コードを確認"画面が表示されました。

以下が”CDK TypeScript コード”欄の内容

Here is the generated infrastructure as code:

import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    
    // Get default VPC id
    const defaultVpc = ec2.Vpc.fromLookup(this, 'DefaultVpc', {
      isDefault: true  
    });
    
    // Create security group
    const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { 
      vpc: defaultVpc,
      allowAllOutbound: true,  
      securityGroupName: 'launch-wizard-1',
      description: 'launch-wizard-1 created 2023-11-27T06:03:55.886Z'
    });
    
    // Allow SSH access
    securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22));
  }
}

Reasoning:

  • Use CDK to define the stack and import the required constructs
  • Lookup the default VPC using the isDefault flag
  • Create a new security group in the default VPC
  • Set the security group name and description based on the CLI
  • Allow ingress rule for SSH based on the authorize-security-group-ingress CLI

以下"CLI コマンド (1 アクション)"の欄の内容

aws ec2 describe-account-attributes --attribute-names "default-vpc" "supported-platforms" 
aws ec2 create-security-group --group-name "launch-wizard-1" --description "launch-wizard-1 created 2023-11-27T06:03:55.886Z" --vpc-id "vpc-6c36a311" 
aws ec2 authorize-security-group-ingress --group-id "sg-023ceefeb3fde310a" --ip-permissions '{"IpProtocol":"tcp","FromPort":22,"ToPort":22,"IpRanges":[{"CidrIp":"0.0.0.0/0"}]}' 

🔻
予め、先ほど作成されたリソースを削除。
🔻

$ mkdir cosole-to-code_test && cd cosole-to-code_test
$ cdk init --language typescript

🔻
lib > cosole-to-code_test-stack.tsの中身を先ほど生成されたコードに置き換えて、想定していた程度の書き換えを行いデプロイしてみます。
🔻

$ cdk deploy --all

✨  Synthesis time: 5.05s

CosoleToCodeTestStack: deploying... [1/1]
CosoleToCodeTestStack: creating CloudFormation changeset...

 ✅  CosoleToCodeTestStack

✨  Deployment time: 32.35s

Stack ARN:
arn:aws:cloudformation:us-east-1:xxxxxxxxxxxx:stack/CosoleToCodeTestStack/xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx

✨  Total time: 37.4s

🔻
無事SecurityGroupが作成されました。

🔻
そろそろインスタンスのActionも生成されているかなと思い、Console-to-Code コンソールに戻ると、インスタンス作成のアクションどころか、先ほどまで表示されていたアクションも見当たらなくなりました。

公式ドキュメントを見ると、

注記
現在のセッション中に実行されたアクションのみがリストされます。以前のセッション中に実行されたアクションは保持されません。

とあります。私の理解が浅いだけかもしれないですが、ここでいう「セッション」はログイン後、ログアウトするまでという事ではないのかもしれないですね。


以上でした

有難うございました。

Discussion