0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VPCエンドポイントをCloudFormationで一括作成:SSM・SSMMessages・EC2Messages対応テンプレート

Posted at

はじめに

本記事では、AWS CloudFormation を用いて、Session Manager 利用に必要な VPC エンドポイント(Interface型)を一括で作成するテンプレートを試してみた記録をまとめます。対象は以下の3つのサービスです:

  • SSM
  • SSMMessages
  • EC2Messages

これらを一つのサブネットに対してまとめて構築できるテンプレートを作成してみました...!

書こうと思ったきっかけ

普段、Session Manager を使って EC2 に接続する際、VPCエンドポイント(特にインターフェース型)を手動で構築していました。

ただ、構成管理や再現性の観点から、毎回マネコンで作るのは非効率だと感じており、CloudFormation テンプレートで一元管理した方が便利だと思いました。

また、Session Manager を使って EC2 に接続する記事は以下の記事で詳細にまとめているので、興味のある方は参考にしてみてください。

そこで、Session Manager に必要な3種のエンドポイントをまとめて作成できるテンプレートを用意し、実際に動かしてみることにしました。

実際にやってみた

以下のテンプレートを使用して、CloudFormation スタックを作成しました。

AWSTemplateFormatVersion: '2010-09-09'
Description: Create SSM-related Interface VPC Endpoints with one subnet

Parameters:
  VpcId:
    Type: AWS::EC2::VPC::Id
    Description: VPC ID

  InterfaceSubnetId:
    Type: AWS::EC2::Subnet::Id
    Description: Interface Subnet ID (1つだけ)

  InterfaceSecurityGroupId:
    Type: AWS::EC2::SecurityGroup::Id
    Description: Interface型エンドポイント用のセキュリティグループID

Resources:

  # SSM Endpoint
  ssmEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssm"
      SubnetIds:
        - !Ref InterfaceSubnetId
      VpcId: !Ref VpcId
      VpcEndpointType: Interface
      SecurityGroupIds:
        - !Ref InterfaceSecurityGroupId
      PrivateDnsEnabled: true

  # SSM Messages Endpoint
  ssmMessagesEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssmmessages"
      SubnetIds:
        - !Ref InterfaceSubnetId
      VpcId: !Ref VpcId
      VpcEndpointType: Interface
      SecurityGroupIds:
        - !Ref InterfaceSecurityGroupId
      PrivateDnsEnabled: true

  # EC2 Messages Endpoint
  ec2MessagesEndpoint:
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ec2messages"
      SubnetIds:
        - !Ref InterfaceSubnetId
      VpcId: !Ref VpcId
      VpcEndpointType: Interface
      SecurityGroupIds:
        - !Ref InterfaceSecurityGroupId
      PrivateDnsEnabled: true

実行手順

  • AWS マネジメントコンソールまたは AWS CLI からスタックを作成

Screenshot 2025-06-15 at 15.54.58.png

  • パラメータに VpcIdInterfaceSubnetIdInterfaceSecurityGroupId を入力

Screenshot 2025-06-15 at 15.56.14.png

  • 数分で 3つのインターフェース型エンドポイントが自動作成されていることが確認できました

Screenshot 2025-06-15 at 15.58.31.png

  • VPC 内の EC2 から Session Manager による接続が即可能な状態に!

Screenshot 2025-06-15 at 15.58.58.png

こんな感じでアクセスできました!

Screenshot 2025-06-15 at 16.50.26.png

特に問題なく一括作成でき、CloudFormation での一元管理の利便性を実感しました...!

まとめ

Session Manager を使う上で必須となる3つの VPC エンドポイントを CloudFormation でまとめて構築するテンプレートを使ってみました。

手動での作業を減らし、再現性やチーム開発での共有も簡単になるので、今後はこのテンプレートをベースに活用して行けたらいいなぁと考えています!

今後は、複数サブネット対応版や、他のサービス向けエンドポイントのテンプレート化も検討していきます...!

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?