よく使いそうなインターフェイスVPCエンドポイントをCloudFormationで一撃設定する

2019.06.04

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、コカコーラ大好きカジです。

インターフェイスVPCエンドポイントをVPC構築済みの環境や、複数VPCに構築するときに使えるCloudFormationテンプレートを作成してみました。

インターフェイスVPCエンドポイントをご存知ですか?不明の方は、以下のブログにわかりやすく記載があるので参照してください。

PrivateLinkがリリースし新たにEC2, Systems Manager, ELB, Kinesis, Service CatalogがVPCエンドポイントに対応しました

CloudWatch が VPC Endpoint に対応!プライベートサブネットからカスタムメトリクスが PUTできます

よく使いそうな、以下のエンドポイントを一撃で構築します。 他のエンドポイントにも流用できると思いますので、どなたかのお役に立てれば光栄です。

  • Cloudwatch
  • Cloudwatch Events
  • Cloudwatch Logs
  • KMS
  • SSM
  • KMS
  • ECR

前提条件

以下のリソースがが作成済みであること

  • VPC
  • サブネット
  • インターフェイスVPCエンドポイントが利用するセキュリティグループ(VPC内の必要なリソースからのアクセス許可が良いと思います。)

インターフェイスVPCエンドポイントの構築

文末のCloudFormationテンプレートでCreate Stackしてください。 Crate Stack時に、VPC、Subnet、Security Groupを指定するようにしています。そのほかはそのままでOKです。

そのまま次へ

スタックの作成を行います。

作成中の状況

作成完了の状況

VPCのコンソールの結果

CloudFormationテンプレート

sample-InterfaceEndpoint-template.yaml

AWSTemplateFormatVersion: '2010-09-09'
# ------------------------------------------------------------#
# Input Parameters
# ------------------------------------------------------------#
Parameters:
#VPCID
  VpcId:
    Description : "VPC ID"
    Type: AWS::EC2::VPC::Id

#InterfaceSubnet1
  InterfaceSubnetId1:
    Description : "Interface Subnet 1st"
    Type : AWS::EC2::Subnet::Id

#InterfaceSubnet2
  InterfaceSubnetId2:
    Description : "Interface Subnet 2st"
    Type : AWS::EC2::Subnet::Id

#Interface Security Group
  InterfaceSecurityGroupId:
    Type: AWS::EC2::SecurityGroup::Id

# ------------------------------------------------------------#
# Create Logs End Point
# ------------------------------------------------------------#
Resources:
 logsEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .logs
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

# ------------------------------------------------------------#
# Create monitoring End Point
# ------------------------------------------------------------#
 MonitoringEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .monitoring
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

# ------------------------------------------------------------#
# Create events End Point
# ------------------------------------------------------------#
 eventsEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .events
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

# ------------------------------------------------------------#
# Create ssm End Point
# ------------------------------------------------------------#
 ssmEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .ssm
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

# ------------------------------------------------------------#
# Create kms End Point
# ------------------------------------------------------------#
 kmsEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .kms
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

# ------------------------------------------------------------#
# Create ecr End Point
# ------------------------------------------------------------#
 ecrEndpoint:
  Type: AWS::EC2::VPCEndpoint
  Properties:
    ServiceName: !Join
      - ''
      - - com.amazonaws.
        - !Ref 'AWS::Region'
        - .ecr.dkr
    SubnetIds:
      - !Ref InterfaceSubnetId1
      - !Ref InterfaceSubnetId2
    VpcId: !Ref 'VpcId'
    VpcEndpointType: Interface
    SecurityGroupIds:
      - !Ref InterfaceSecurityGroupId
    PrivateDnsEnabled: true

参考元

Creating AWS VPC Endpoints with CloudFormation