通过SSM访问EC2
背景 – context
上次,我第一次使用Terraform成功地在AWS EC2上构建了一个实例。
通过管理控制台,我确认了构建的情况,并通过SSH连接验证了对实例的连接。
然而,在工作中,要构建的EC2禁止使用SSH连接,必须使用Session Manager(以下简称SSM)进行连接。
虽然我对SSM有所了解,但从未使用过,所以我打算借此机会尝试一下。
目标
使用Terraform构建可与SSM连接的EC2实例。
环境
使用上次执行Terraform时的Docker容器环境。
但是,由於這次要使用SSM,所以需要安裝插件。
在Dockerfile中添加以下內容,然後啟動容器。
# install session-manager-plusin
RUN curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
RUN dpkg -i session-manager-plugin.deb
请看以下参考答案。
使用Terraform构建可通过Systems Manager(Session Manager)访问的EC2实例
(Option)安装用于AWS CLI的Session Manager插件
文件结构
terraform
└terraform.tfvars
└variables.tf
└ec2.tf
└role.tf
改造
└改造.tfvars
└变量.tf
└ec2.tf
└角色.tf
步骤
基本的说,Terraform文件的结构没有改变。
重点在于创建SSM角色并将其关联到实例上。
角色.tf
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "role" {
name = "xxxxx_ssm_MyRole"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
data "aws_iam_policy" "systems_manager" {
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "default" {
role = aws_iam_role.role.name
policy_arn = data.aws_iam_policy.systems_manager.arn
}
resource "aws_iam_instance_profile" "systems_manager" {
name = "xxxxx_MyInstanceProfile"
role = aws_iam_role.role.name
}
ec2.tf的中文释义
我只是在之前的Terraform文件中添加了“iam_instance_profile”的设置。
resource "aws_instance" "c069544_tf-ec2" {
count = 1
ami = "ami-03d5c68bab01f3496" # Ubuntu 20.04 LTS official ami
instance_type = "t2.micro"
iam_instance_profile = aws_iam_instance_profile.systems_manager.name
tags = {
Name = "${format("xxxxx_tf-ec2-%02d", count.index + 1)}"
}
}
建立
计划
# terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create
Terraform will perform the following actions:
:
:
}
}
Plan: 4 to add, 0 to change, 0 to destroy.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if
you run "terraform apply" now.
办理
这次我们也试试使用“-auto-approve”选项在自动化环境下进行手动构建确认。
# terraform apply -auto-approve
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
+ create
Terraform will perform the following actions:
:
:
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
展示
我运行了 terraform show 命令进行确认,看起来已经成功构建了。(省略执行结果)
请确认连接
由于似乎成功构建了,我会立即尝试进行SSM连接。
首先执行魔法般的aws配置命令。
# aws configure
AWS Access Key ID []:
AWS Secret Access Key []:
Default region name []:
Default output format []:
接下来,我们尝试获取EC2实例的列表。
# aws ec2 describe-instances --output=table --query 'Reservations[].Instances[].{InstanceId: InstanceId, PrivateIp: join(`, `, NetworkInterfaces[].PrivateIpAddress), GlobalIP: join(`, `, NetworkInterfaces[].Association.PublicIp), Platform:Platform, State: State.Name, SecurityGroupId: join(`, `, SecurityGroups[].GroupId) ,Name: Tags[?Key==`Name`].Value|[0]}'
----------------------------------------------------------------------------------------------------------------------------
| DescribeInstances |
+-------------+----------------------+--------------------+-----------+---------------+------------------------+-----------+
| GlobalIP | InstanceId | Name | Platform | PrivateIp | SecurityGroupId | State |
+-------------+----------------------+--------------------+-----------+---------------+------------------------+-----------+
| xx.xx.xx.xx| xxxxxxxxxx | xxxxx_tf-ec2-01 | None | xx.xx.xx.xx | xxxxxxxxxx | running |
+-------------+----------------------+--------------------+-----------+---------------+------------------------+-----------+
由于确认已经成功建立,可以使用InstanceId来连接SSM。
# aws ssm start-session --target xxxxx
Starting session with SessionId: xxxxx_xxxxx-xxxxx
$ ls -al
total 8
drwxr-xr-x 2 root root 4096 Apr 30 23:36 .
drwxr-xr-x 4 root root 4096 Apr 30 23:36 ..
$ uname -a
Linux ip-172-31-14-64 5.4.0-1045-aws #47-Ubuntu SMP Tue Apr 13 07:02:25 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
我确认成功地连接了SSM。
最后
这次验证过程中,我们在没有访问Management Console的情况下完成了搭建工作。
当然,在完成搭建验证后,我们执行了terraform destroy命令,销毁了环境。
我感到很高兴能够利用之前使用过的Terraform工具进行修改,将其应用于本次验证,并且能够为今后的发展提供可能性。
我們正在與同伴一起進行CICD環境的構建。我們正考慮將本次驗證的Terraform集成到CICD中,以便在CICD流程中進行AWS環境的構建。
我们计划继续进行一项能够让成员们逐渐提升技能的活动,就是从小事做起,一边进行讨论,共享彼此的验证结果,一边实际动手、验证和体验。