3-part series - (1) Launch an EC2 Instance, Install CodeDeploy Agent and upload App_Linux.zip file to version enabled S3 bucket
Tags: codedeploy, s3, ec2, codepipeline
Published: 2023-02-21 02:43:05

In this **Create and Deploy applications using CodeDeploy and CodePipeline with Amazon S3 bucket versioning enabled** -

In the **1st article**, We will create, SSH into EC2 Instance and Install CodeDeploy Agent. Then Create a S3 bucket with versioning enabled, upload App_Linux.zip

_**Let’s get started!**_

Please visit my **[GitHub Repository for CI-CD/Docker/ECS/ECR articles](https://github.com/awsmine/Docker_projects)** on various topics being updated on constant basis.

# Objectives:

**1.** Sign in to AWS Management Console

**2.** Launch an EC2 instance, Connect (SSH) to EC2 Instance thru Putty 

**3.** Install CodeDeploy Agent

**4.** Create S3 Bucket, upload **`App_Linux.zip`** and Copy the Object's key

# Pre-requisites:
- AWS user account with admin access, not a root account.
- AWS CLI.


# Resources Used:

**[CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)**

**[CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html)**

**[CodeDeploy](https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html)**

**[AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)**

# Steps for implementation to this project

## 1. Sign in to AWS Management Console

- Log into the AWS Management Console, AWS Region as US East (N. Virginia) us-east-1.

## 2. Launch an EC2 instance, Connect (SSH) to EC2 Instance thru Putty

**Launch an EC2 instance**

- **`Go to EC2 Dashboard, Launch instance, my-ec2, Select t2.micro. NVirKey, default vpc, subnets - no preference, Auto-assign public IP - enable, Create Security group, CodePipeline-SG, SSH with 0.0.0.0/0, Add security group rule, HTTP, 80, 0.0.0.0/0`**

- **`Attach Role - Role-EC2Instance with attched policyAmazonEC2RoleforAWSCodeDeploy`**

- Launch instance



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




- **Connect (SSH) to EC2 Instance thru Putty**



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




## 3. Install CodeDeploy Agent

- **`Switch to root user`**

``` 

yum update -y
```

- **`Before Installing CodeDeploy Agent, Install the prerequisite i.e. ruby and wget`**

```
yum install ruby wget -y
```

- **`Download the CodeDeploy agent's installer`**

```
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
```

- **`Change the permission of the installer`**

```
chmod +x ./install
```


- **`Install the latest version of the CodeDeploy agent`**

```
sudo ./install auto
```


- **`Check status`**

```
service codedeploy-agent status
```

- Note: If the output is "The AWS CodeDeploy agent is running as PID 3523" or any other PID. It means codedeploy agent is running fine.



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



- If the output says, "error: No AWS CodeDeploy agent running", then start the agent by running the below command and check the status again.

- **`Start the agent`**

```
service codedeploy-agent start
```


- **`check the status`**

```
service codedeploy-agent status 
```


## 4. Create S3 Bucket, upload **`App_Linux.zip`** and Copy the Object's key


- **`On S3 Console, Create bucket, codecommit-bucket12, Uncheck Block all public access, Bucket Versioning - Enable`**

- Create bucket

- **`Click the bucket, Upload, Addfiles, App_Linux.zip, Upload`**


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

- **App_Linux.zip** - has the following files

- **appspec.yaml**
```
version: 0.0
os: linux
files:
  - source: /index.html
    destination: /var/www/html/
hooks:
  BeforeInstall:
    - location: scripts/install_dependencies
      timeout: 300
      runas: root
    - location: scripts/start_server
      timeout: 300
      runas: root
  ApplicationStop:
    - location: scripts/stop_server
      timeout: 300
      runas: root
```

- **index.html**
```
<!doctype html>
<html>
  <body>
    <h1>This is my Codepipeline from S3 bucket! </h1>
  </body>
</html>
```


- **scripts folder has 3 files**

**install_dependencies**


```
#!/bin/bash
yum install -y httpd
```


**start_server**

```
#!/bin/bash
service httpd start
```

**stop_server**

```
#!/bin/bash
service httpd stop
```

- Copy the object Key, and save it to notepad

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


# What we have done so far

- We have successfully created, SSH into EC2 Instance and Installed CodeDeploy Agent. Then Created a S3 bucket with versioning enabled, uploaded App_Linux.zip