この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
Systems Manager Run Command をプライベートサブネット上の EC2インスタンスに対して実行する手順
を以前紹介しました。
Systems Manager Run Command をプライベートサブネット上の EC2インスタンスに対して実行する手順と注意点
こちら、VPCエンドポイントを4つ作成しますが マネジメントコンソールからの作成は少々面倒です。
そこで、これらSSM用のVPCエンドポイントを CloudFormation テンプレートとして作成してみました。
プライベートEC2インスタンスを持っていて、
- 普段そんなにコンソール入らない・・・
- 踏み台はセキュリティ面でもコスト面でもやだ・・・
- VPCエンドポイントも 時間ごとの料金 がかかるから常時置きたくない・・・
- けど今ちょっと RunCommand/SessionManager でいじりたい・・・
そんなかたにオススメです。
CloudFormation テンプレート
テンプレートを載せます。 RunCommand/SessionManager で必要なVPCエンドポイント (+セキュリティグループ) を作成します。
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Tempolarily make VPC Endpoints required for SSM RunCommand/SessionManager'
Parameters:
VpcId:
Description : "VPC of the EC2 instance"
Type: AWS::EC2::VPC::Id
SubnetId:
Description : "Subnet of the EC2 instance"
Type : AWS::EC2::Subnet::Id
SecurityGroupId:
Description : "SecurityGroup of the EC2 instance"
Type: AWS::EC2::SecurityGroup::Id
Resources:
SecurityGroupForEndpoint:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Only allow HTTPs traffic from the EC2 security group
SecurityGroupIngress:
- SourceSecurityGroupId: !Ref SecurityGroupId
IpProtocol: "tcp"
FromPort: 443
ToPort: 443
VpcId: !Ref VpcId
SsmEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub com.amazonaws.${AWS::Region}.ssm
SubnetIds:
- !Ref SubnetId
VpcId: !Ref 'VpcId'
VpcEndpointType: Interface
SecurityGroupIds:
- !Ref SecurityGroupForEndpoint
PrivateDnsEnabled: true
Ec2MessagesEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub com.amazonaws.${AWS::Region}.ec2messages
SubnetIds:
- !Ref SubnetId
VpcId: !Ref 'VpcId'
VpcEndpointType: Interface
SecurityGroupIds:
- !Ref SecurityGroupForEndpoint
PrivateDnsEnabled: true
SsmMessagesEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub com.amazonaws.${AWS::Region}.ssmmessages
SubnetIds:
- !Ref SubnetId
VpcId: !Ref 'VpcId'
VpcEndpointType: Interface
SecurityGroupIds:
- !Ref SecurityGroupForEndpoint
PrivateDnsEnabled: true
LogsMessagesEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
ServiceName: !Sub com.amazonaws.${AWS::Region}.logs
SubnetIds:
- !Ref SubnetId
VpcId: !Ref 'VpcId'
VpcEndpointType: Interface
SecurityGroupIds:
- !Ref SecurityGroupForEndpoint
PrivateDnsEnabled: true
パラメータ
スタックを作成する際は、EC2インスタンスが所属する VPC , サブネット , セキュリティグループ を指定してください。
使用
※ Run Command ではコマンド出力先は CloudWatch にしてください。
おわりに
このテンプレートが少しでもどなたかのお役に立てば幸いです。