AL2023 with Ruby, Python, Node.js, and Rust
EC2 環境を AL2023(Amazon Linux 2023) に切り替えるべく準備を進めています。AL2025 がリリースされる 2025年まで の 2 年間のベースとする予定です。
まずは作って壊せるスタンドアロンの開発環境(workstation)からはじめます。私の場合 1. VPC, Subnet, RouteTable, InternetGateway ... etc のレイヤは CloudFormation が担当し、2. EC2 や RDS のレイヤを ansible が担当します。個人的な開発環境の動作要件は以下になります。
- mandatory:
- Ruby, Python, Node.js, Rust の最新バージョンが利用可能であること
- 必要なツール群が利用可能であること(jq, rg, fd, fzf)
- optional:
- ec2 instance connect で接続できること
僕が作った最強の user_data
こちらの user_data.txt を t4g.small で走らせると、インスタンスの起動から構築完了までの所要時間は約 20 分でした。
➜ pyenv exec ansible-playbook -i hosts site.yml --extra-vars "@workstation.json"

➜ cat roles/spawn/tasks/main.yml
---
- name: Spawn an ec2 instance
amazon.aws.ec2_instance:
name: "{{ instance_name }}"
key_name: "{{ key_name }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
instance_type: "{{ instance_type }}"
security_groups: "{{ security_groups }}"
network:
assign_public_ip: true
private_ip_address: "{{ private_ip_address }}"
image_id: "{{ image_id }}"
user_data: "{{ lookup('file', 'user_data.txt') }}"
volumes:
- device_name: /dev/xvda
ebs:
volume_size: 30
volume_type: gp2
delete_on_termination: true
tags:
Owner: "{{ owner }}"
Stack: "{{ stack }}"
Project: "{{ project }}"
Application: "{{ application }}"
Ec2InstanceConnectAccess: "{{ ec2_instance_connect_access }}"
➜ cat workstation/workstation.json
{
"instance_name": "workstation",
"key_name": "__key-name__",
"vpc_subnet_id": "__subnet-id__",
"instance_type": "t4g.small",
"security_groups": ["__sg-id1__", "__sg-id2__"],
"private_ip_address": "10.0.0.10",
"image_id": "resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64",
"owner": "kyagi",
"stack": "deveplopment",
"project": "workstation",
"application": "hodgepodge of development tools",
"ec2_instance_connect_access": "true"
}
シンボリックリンク的に指定できるイメージ ID 設定
上記の workstation.json では resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64 のようにイメージ ID を指定しています。AL2023 から(?) image id がシンボリックリンク的に指定できるようになっています。リンク先の image id は自動的に最新のものが参照されるので ansible や Cfn で更新をする必要がなくなりました。
The following are dynamic kernel specifications. The default kernel version automatically changes with each major kernel version update. al2023-ami-kernel-default-arm64 for arm64 architecture al2023-ami-minimal-kernel-default-arm64 for arm64 architecture (minimal AMI) al2023-ami-kernel-default-x86_64 for x86_64 architecture al2023-ami-minimal-kernel-default-x86_64 for x86_64 architecture (minimal AMI) https://docs.aws.amazon.com/linux/al2023/ug/get-started.html
EC2 instance connect 用のタグ設定
タグ Ec2InstanceConnectAccess: true が指定された EC2 は AWS Management Console から EC2 Instance Connect ができるように設定します。別途 Ec2 Instance Connect にはリージョンごとの特定の IP 範囲からの接続をセキュリティグループで許可する必要がある で紹介した IAM 設定が必要です。
参考情報
- Amazon Linux 2023, a Cloud-Optimized Linux Distribution with Long-Term Support | AWS News Blog
- amazon.aws.ec2_instance module – Create & manage EC2 instances — Ansible Documentation
- GitHub - ansible-collections/amazon.aws: Ansible Collection for Amazon AWS
- Get started with Amazon Linux 2023 - Amazon Linux 2023
- Run commands on your Linux instance at launch - Amazon Elastic Compute Cloud