Home > OS >  How do I setup EC2 instance Connect using CloudFormation?
How do I setup EC2 instance Connect using CloudFormation?

Time:11-02

I am trying to set up EC2 Instance Connect for an EC2 instance:

AWSTemplateFormatVersion: 2010-09-09
Description: Part 1 - Spawn Ec2 instance with CloudFormation

Resources:
  WebAppInstance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-east-2a
      ImageId: ami-074cce78125f09d61
      InstanceType: t2.micro

Although the template above allows me to create an EC2 instance, it does not allow me to access it using EC2 Instance Connect.

enter image description here

How do I configure EC2 Instance Connect within the CloudFormation template?

Solution

AWSTemplateFormatVersion: 2010-09-09
Description: Part 1 - Build a webapp stack with CloudFormation

Resources:
  WebAppInstance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-east-2a
      ImageId: ami-074cce78125f09d61
      InstanceType: t2.micro
      SecurityGroupIds:
        - !Ref WebAppSecurityGroup

  WebAppSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: !Join ["-", [webapp-security-group, dev]]
      GroupDescription: "Allow HTTP/HTTPS and SSH inbound and outbound traffic"
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0

  WebAppEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc
      InstanceId: !Ref WebAppInstance
      Tags:
        - Key: Name
          Value: !Join ["-", [webapp-eip, dev]]

Outputs:
  WebsiteURL:
    Value: !Sub http://${WebAppEIP}
    Description: WebApp URL

CodePudding user response:

On Amazon Linux 2 (any version) and Ubuntu 16.04 or later EC2 Instance Connect is installed and working by default. So you don't have to do anything.

For other AMIs, you have to use user_data to install and setup the connect yourself.

CodePudding user response:

Ensure you have a public IP assigned.

As per docs:

To connect using the Amazon EC2 console (browser-based client), the instance must have a public IPv4 address.**


You can also connect to the EC2 instance via other methods if you do not want to / cannot assign a public IPv4 address:

If the instance does not have a public IP address, you can connect to the instance over a private network using an SSH client or the EC2 Instance Connect CLI. For example, you can connect from within the same VPC or through a VPN connection, transit gateway, or AWS Direct Connect.


FYI: for other AMIs with Linux distributions other than Amazon Linux 2 or Ubuntu 16.04 , you will need extra configuration as Marcin's answer points out.

ami-074cce78125f09d61 in us-east-2 is coming up for me as Amazon Linux 2 AMI (HVM), SSD Volume Type which supports EC2 Instance Connect by default, so your AMI should be fine.

  • Related