AWS Systems Managerを使ってリモート接続をしてみた

## 概要

AWS Systems Managerを使ってPrivate subnetにあるEC2にリモート接続をしてみた

![スクリーンショット 2024-04-07 19.43.00 1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3747730/64afb5e8-71e7-b482-8083-dc75233602d9.png)

https://pages.awscloud.com/JAPAN-event-OE-Hands-on-for-Beginners-systems-manager-2022-reg-event.html

## マネージドインスタンスにする3つのSTEP
※ セミナーの資料を引用しています

### STEP1 SSM Agentの導入
![スクリーンショット 2024-04-07 19.56.19.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3747730/4d4d1555-bcb2-6a8a-8b9a-7f697770b15d.png)

### STEP2 アウトバウンド経路を作成
![スクリーンショット 2024-04-07 19.56.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3747730/b6a6cd9c-cda2-4d46-ada5-2ff903e410db.png)

### STEP3 IAMロールの付与

![スクリーンショット 2024-04-07 19.56.49.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3747730/71956f07-c922-a87e-00d8-6930570a2258.png)

## CloudFormation
※セミナーの内容と完全に一致していません

```yml
AWSTemplateFormatVersion: "2010-09-09"
Description: "This template is for 'AWS Hands-on for Beginners Systems Manager Hands-on'."
Parameters: 
  VPCCidr: 
    Type: "String"
    Default: "10.0.0.0/16"
    Description: "VPCCidr"
  PublicSubnetCidr: 
    Type: "String"
    Default: "10.0.0.0/24"
    Description: "PublicSubnetCidr"
  PrivateSubnetCidr: 
    Type: "String"
    Default: "10.0.1.0/24"
    Description: "PrivateSubnetCidr"
Resources: 
  VPC: 
    Type: "AWS::EC2::VPC"
    Properties: 
      CidrBlock: 
        Ref: "VPCCidr"
      EnableDnsSupport: "true"
      EnableDnsHostnames: "true"
      Tags: 
      - Key: "Name"
        Value: "h4b-vpc"
        
  PublicSubnet: 
    Type: "AWS::EC2::Subnet"
    Properties: 
      VpcId: 
        Ref: "VPC"
      CidrBlock: 
        Ref: "PublicSubnetCidr"
      AvailabilityZone: 
        Fn::Select: 
        - "0"
        -
          Fn::GetAZs: 
            Ref: "AWS::Region"
      MapPublicIpOnLaunch: "true"
      Tags: 
      - Key: "Name"
        Value: "h4b-public-subnet"
        
  PrivateSubnet: 
    Type: "AWS::EC2::Subnet"
    Properties: 
      VpcId: 
        Ref: "VPC"
      CidrBlock: 
        Ref: "PrivateSubnetCidr"
      AvailabilityZone: 
        Fn::Select: 
        - "1"
        -
          Fn::GetAZs: 
            Ref: "AWS::Region"
      Tags: 
      - Key: "Name"
        Value: "h4b-private-subnet"
        
  EIP: 
    Type: "AWS::EC2::EIP"
    Properties: 
      Tags: 
      - Key: "Name"
        Value: "h4b-eip"
        
  InternetGateway: 
    Type: "AWS::EC2::InternetGateway"
    Properties: 
      Tags: 
      - Key: "Name"
        Value: "h4b-igw"
        
  AttachGateway: 
    Type: "AWS::EC2::VPCGatewayAttachment"
    Properties: 
      VpcId: 
        Ref: "VPC"
      InternetGatewayId: 
        Ref: "InternetGateway"
        
  NATGateway: 
    Type: "AWS::EC2::NatGateway"
    Properties: 
      AllocationId: 
        Fn::GetAtt: 
        - "EIP"
        - "AllocationId"
      ConnectivityType: "public"
      SubnetId: 
        Ref: "PublicSubnet"
        
  RouteTableforPublic: 
    Type: "AWS::EC2::RouteTable"
    Properties: 
      VpcId: 
        Ref: "VPC"
      Tags: 
      - Key: "Name"
        Value: "h4b-public-route-table"
        
  RouteTableforPrivate: 
    Type: "AWS::EC2::RouteTable"
    Properties: 
      VpcId: 
        Ref: "VPC"
      Tags: 
      - Key: "Name"
        Value: "h4b-private-route-table"
        
  RouteForPublic: 
    Type: "AWS::EC2::Route"
    DependsOn: "AttachGateway"
    Properties: 
      RouteTableId: 
        Ref: "RouteTableforPublic"
      DestinationCidrBlock: "0.0.0.0/0"
      GatewayId: 
        Ref: "InternetGateway"
        
  RouteForPrivate: 
    Type: "AWS::EC2::Route"
    DependsOn: "NATGateway"
    Properties: 
      RouteTableId: 
        Ref: "RouteTableforPrivate"
      DestinationCidrBlock: "0.0.0.0/0"
      NatGatewayId: 
        Ref: "NATGateway"
        
  SubnetRouteTableAssociation1: 
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties: 
      SubnetId: 
        Ref: "PublicSubnet"
      RouteTableId: 
        Ref: "RouteTableforPublic"
        
  SubnetRouteTableAssociation2: 
    Type: "AWS::EC2::SubnetRouteTableAssociation"
    Properties: 
      SubnetId: 
        Ref: "PrivateSubnet"
      RouteTableId: 
        Ref: "RouteTableforPrivate"
        
  EC2SecurityGroup: 
    Type: "AWS::EC2::SecurityGroup"
    Properties: 
      VpcId: 
        Ref: "VPC"
      GroupDescription: "No Inbound Rule Security Group"
      GroupName: "h4b-ec2-sg"
      Tags: 
      - Key: "Name"
        Value: "h4b-ec2-sg"
        
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0bdd30a3e20da30a1
      SecurityGroupIds: 
        - !Ref EC2SecurityGroup
      AvailabilityZone: 
        Fn::Select: 
        - "1"
        -
          Fn::GetAZs: 
            Ref: "AWS::Region"
      IamInstanceProfile: !Ref InstanceProfile
      InstanceType: t2.micro
      SubnetId: !Ref PrivateSubnet
      Tags:
        - Key: Name
          Value: h4b-instance

  InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles: 
        - !Ref EC2SSMRole

  EC2SSMRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
        - Action: 'sts:AssumeRole'
          Effect: Allow
          Principal:
            Service: ec2.amazonaws.com
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
```