How to deploy app to aws server
Deploying an application to AWS servers involves several steps, depending on the specific service you are using (e.g., EC2, ECS, EKS, Lambda). Here, I'll provide a guide for deploying a simple web application using different AWS services:
1. Deploying to EC2 (Elastic Compute Cloud)
Step 1: Set Up an EC2 Instance
-
Launch an EC2 Instance:
- Go to the EC2 Dashboard.
- Click on "Launch Instance."
- Choose an Amazon Machine Image (AMI) (e.g., Amazon Linux 2).
- Choose an instance type (e.g., t2.micro for the free tier).
- Configure instance details, add storage, add tags if needed.
- Configure the security group to allow traffic on port 22 (SSH) and the port your application will use (e.g., 80 for HTTP).
- Review and launch.
-
Connect to Your EC2 Instance:
- Open your terminal (or use an SSH client).
- Connect using the SSH command provided in the EC2 console:
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns
Step 2: Deploy Your Application
-
Install Necessary Software:
- Update the package list and install necessary software (e.g., Node.js for a Node.js application):
sudo yum update -y sudo yum install -y nodejs npm
- Update the package list and install necessary software (e.g., Node.js for a Node.js application):
-
Upload Your Application:
- You can use
scp
to copy files to your instance:scp -i /path/to/your-key-pair.pem -r /path/to/your-app ec2-user@your-ec2-public-dns:/home/ec2-user/
- You can use
-
Run Your Application:
- Navigate to your application directory and install dependencies:
cd /home/ec2-user/your-app npm install
- Start your application (you might want to use a process manager like
pm2
):npm start
- Navigate to your application directory and install dependencies:
-
Configure Your Application to Run on Startup:
- Install
pm2
:sudo npm install -g pm2
- Start your application with
pm2
:pm2 start npm -- start pm2 startup systemd pm2 save
- Install
2. Deploying to ECS (Elastic Container Service)
Step 1: Set Up ECS
-
Create a Docker Image:
- Create a
Dockerfile
in your application directory:FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD ["node", "index.js"]
- Build and push the Docker image to Amazon ECR (Elastic Container Registry):
aws ecr create-repository --repository-name your-repo-name docker build -t your-repo-name . $(aws ecr get-login --no-include-email --region your-region) docker tag your-repo-name:latest your-account-id.dkr.ecr.your-region.amazonaws.com/your-repo-name:latest docker push your-account-id.dkr.ecr.your-region.amazonaws.com/your-repo-name:latest
- Create a
-
Create an ECS Cluster:
- Go to the ECS Dashboard.
- Click on "Create Cluster" and follow the wizard to create a cluster.
Step 2: Deploy Your Application to ECS
-
Create a Task Definition:
- Go to the Task Definitions tab.
- Click on "Create new Task Definition."
- Define the container details (e.g., image URI from ECR, port mappings).
-
Run a Task:
- Go to your cluster.
- Click on "Tasks" and then "Run new Task."
- Select your task definition and configure the desired settings.
3. Deploying to Lambda (Serverless)
Step 1: Create a Lambda Function
-
Package Your Code:
- Create a ZIP file containing your application code and dependencies.
-
Create a Lambda Function:
- Go to the Lambda Dashboard.
- Click on "Create function."
- Choose "Author from scratch" or "Use a blueprint."
- Configure the function details (e.g., runtime, handler).
-
Upload Your Code:
- In the function code section, upload the ZIP file.
-
Configure Triggers:
- Configure triggers (e.g., API Gateway for HTTP requests).
Step 2: Deploy and Test
-
Deploy Your Function:
- Click on "Deploy."
-
Test Your Function:
- Use the Lambda console to test your function.
- If using API Gateway, test via the endpoint provided.
Published on: Jul 08, 2024, 10:43 PM