How To SSH Your ECS Server(Running Fargate)
Tags: aws, ecs, docker
Published: 2022-01-12 15:38:30

# [1.Install ACLI V2](#1)
# [2.Install and Run Docker On EC2](#2)
# [3.Use ECR create repository](#3)
# [4.Attach IAM Role To EC2](#4)
# [5.Build and Push Image to Repository](#5)
# [6.Create ECS Task Definition](#6)
# [7.Create Cluster](#7)
# [8.Create Service](#8)
# [9.Use SSH Tool connect your container](#9)
<h2 id="1">1.Install ACLI V2 On Ami-2</h2>

In here,I use Amazon Linux 2, t3.micro instance type.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81pft0xoa0r89hoks191.png)
 
First,We need give the instance right IAM Role

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxx7bsi71q1aimqpnodb.png) 

Install ACLI V2,I will put the code

```
rm -rf /bin/aws
rm -rf /bin/aws*
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
ln -s /usr/local/bin/aws /bin/aws

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmtwqlreo80fsp6fm4fj.png)



```
[root@ip-172-31-43-69 ~]# yum install docker && systemctl start docker
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwkknrdgze3hn1vo5yo1.png)

<h2 id=3>Use ECR create repository</h2>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4o1id8d4n5ynxcg7ibc1.png)
 
<h2 id=4>Attach IAM Role To EC2</h2>
[https://docs.amazonaws.cn/en_us/AmazonECR/latest/userguide/registry-permissions-create.html](https://docs.amazonaws.cn/en_us/AmazonECR/latest/userguide/registry-permissions-create.html)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3weecswiqbo0z2b790z.png)
 

<h2 id=5>Build and Push Image to Repository</h2>
<h3>1.Write Dockerfile</h3>

```
FROM centos:7

RUN yum install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config 
RUN useradd admin  
RUN echo "admin:123456" | chpasswd
RUN echo "admin   ALL=(ALL)       ALL" >> /etc/sudoers 

RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
```
<h3>2.Build Image</h3>

```
[root@ip-172-31-43-69 ~]# docker build -t sshd .
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p452clp1etpj627x0q83.png)
 
<h3>3.Tag and Push Image</h3>

```
aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
docker tag sshd:latest aws_account_id.dkr.ecr.region.amazonaws.com/sshd:latest
docker push aws_account_id.dkr.ecr.region.amazonaws.com/sshd:latest
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm9446iu7y010ovqrauj.png)
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slqv4fl5d8opyxk5l2py.png)
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5ad541y5y2qep0pm94e.png)
 
<h2 id=6>Create Fargate Task Definitions(I put my json code)</h2>
### **Notice:Replace the capital letter identification part**

```
{
  "ipcMode": null,
  "executionRoleArn": "YOUR_EXEC_ROLE_ARN": [
    {
      "dnsSearchDomains": null,
      "environmentFiles": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "secretOptions": null,
        "options": {
          "awslogs-group": "/ecs/sshd_task",
          "awslogs-region": "YOUR_REGION",
          "awslogs-stream-prefix": "ecs"
        }
      },
      "entryPoint": null,
      "portMappings": [
        {
          "hostPort": 22,
          "protocol": "tcp",
          "containerPort": 22
        }
      ],
      "command": null,
      "linuxParameters": null,
      "cpu": 0,
      "environment": [],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": null,
      "memory": null,
      "memoryReservation": 2048,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": "YOU_CREATE_IMG_URL",
      "startTimeout": null,
      "firelensConfiguration": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "pseudoTerminal": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": null,
      "name": "sshd_container"
    }
  ],
  "placementConstraints": [],
  "memory": "2048",
  "taskRoleArn": "USE_YOURSELF_TASK_ROLE",
  "compatibilities": [
    "EC2",
    "FARGATE"
  ],
  "taskDefinitionArn": "USE_YOURSELF_ARN",
  "family": "sshd_task",
  "requiresAttributes": [
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.execution-role-awslogs"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.ecr-auth"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.task-iam-role"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.execution-role-ecr-pull"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
    },
    {
      "targetId": null,
      "targetType": null,
      "value": null,
      "name": "ecs.capability.task-eni"
    }
  ],
  "pidMode": null,
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "networkMode": "awsvpc",
  "cpu": "1024",
  "revision": 2,
  "status": "ACTIVE",
  "inferenceAccelerators": null,
  "proxyConfiguration": null,
  "volumes": []
}
```
<h2 id=7>7.Create Cluster</h2>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va9r3tuqpedo2ldxhq7c.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbi032ei7aq65a0cdke7.png)
 
  
<h2 id=8>8.Create Service</h2>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chokwk702qpzpyn9xxjd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yle8xd0usrvhvw6eyyag.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chgnxux3g7prrg3oxocf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9k871ha9832c2vsne7lo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ssufpsgouoi87z09r5cb.png)
     
<h2 id=9>9.Use SSH Tool connect your container</h2>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0v9qn3kjczvz6nkzgx5x.png)