Git Product home page Git Product logo

aws-url-shortener's Introduction

aws-url-shortener

Table of Contents

What does it do

This project aims to facilitate the creation and deployment of a Docker container a Python URL shortener application running on an AWS ECS cluster, incorporating a LoadBalancer for seamless load distribution.

This project uses / Dependencies

Install

Configuration — Python

Make sure you have the requirements in requirements.txt.

Configuration — Docker container

Upon cloning the code, you will have the capability to build the image and execute the container.

Open a terminal or command prompt, navigate to the directory containing the Dockerfile, and run the following command to build the Docker image:

docker build -t my-url-shortener-app .

Run it locally if you want to test it:

docker run -d --name my-url-shortener-app --sysctl net.ipv4.ip_unprivileged_port_start=0 --publish 8215:8215 my-url-shortener-app

How to test it:

curl -vvvvv http://localhost:8215/my-old-path

Result should be:

*   Trying 127.0.0.1:8215...
* Connected to localhost (127.0.0.1) port 8215 (#0)
> GET /my-old-path HTTP/1.1
> Host: localhost:8215
> User-Agent: curl/7.86.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Server: SimpleHTTP/0.6 Python/3.9.17
< Date: Sun, 02 Jul 2023 21:52:25 GMT
< Location: /caiu-no-else
<
* Closing connection 0

Configuration — ECR

Log in to the AWS Management Console and navigate to the Amazon ECR service.

Create a new repository or choose an existing one to store your Docker image.

Follow the instructions provided by Amazon ECR to create a repository and obtain the necessary credentials to push images to the repository.

In your terminal or command prompt, authenticate Docker with ECR by running the following command and following the prompts:

aws ecr get-login-password --region your-ecr-region | docker login --username AWS --password-stdin your-ecr-account-id.dkr.ecr.your-ecr-region.amazonaws.com

Replace your-ecr-region with the AWS region where your ECR repository is located and your-ecr-account-id with your AWS account ID.

Once authenticated, you can push the Docker image that you have created to ECR by running the following command:

docker tag your-image-name:latest your-ecr-account-id.dkr.ecr.your-ecr-region.amazonaws.com/your-repository-name:latest
docker push your-ecr-account-id.dkr.ecr.your-ecr-region.amazonaws.com/your-repository-name:latest

Replace your-repository-name with the name of the ECR repository you created or chose.

If you haven't done it yet, you will need to setup your AWS CLI credentials in order to be able to push the ECR image.

Configuration — ECS

  1. Create an ECS cluster:

Log in to the AWS Management Console and navigate to the ECS service.

Create a new cluster or choose an existing one to host your containers.

Follow the instructions provided by ECS to create a cluster.

Cluster name: my-url-shortener Choose your VPC and subnets Add tags as you see appropriate Click Create

  1. Create an ECS task definition:

In the ECS service console, navigate to "Task Definitions" and click "Create new Task Definition." Choose the launch type compatible with your requirements (EC2 or Fargate).

Configure the task definition: Enter a name for your task definition: my-url-shortener-task-definition Specify the task execution role if necessary.

Add a container definition: Enter a name for your container. Specify the ECR image URI for your Docker image (e.g., your-ecr-account-id.dkr.ecr.your-ecr-region.amazonaws.com/your-repository-name:latest).

Container: Name: my-url-shortener Image URI: <ECR image>`` Set port 8215`.

Environment: App environment: AWS Fargate CPU: 1vCPU Memory: 3Gb Task role: None

  1. Create an ECS service: In the ECS service console, navigate to Clusters and select your cluster. Click Create to create a new service.

Configure the service: Under Deployment configuration select my-url-shortener-app-task-definition under Family. Under Service name: my-url-shortener-app-service Under Desired tasks select 2. Under Networking select your VPC and subnets. Create a new security group and allow HTTP access to port 80 and TCP access to port 8080. Under Load balacing select Application Load Balancer and then select a name for your Load Balance and a target group name.

You can click View in CloudFormation to check the creation of the ECS Service.

How to test it

Once the creation is completed, click your the service name you choose under Services, then click Networking. There you will find the Load Balancer name under DNS names.

How to test it:

curl -vvvv my-url-shortener-lb-new2-304039992.us-east-1.elb.amazonaws.com/dynamodb

Result should be similar to:

*   Trying 184.73.160.70:80...
* Connected to my-url-shortener-lb-new2-304039992.us-east-1.elb.amazonaws.com (184.73.160.70) port 80 (#0)
> GET /dynamodb HTTP/1.1
> Host: my-url-shortener-lb-new2-304039992.us-east-1.elb.amazonaws.com
> User-Agent: curl/7.86.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Date: Mon, 03 Jul 2023 03:39:32 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< Server: SimpleHTTP/0.6 Python/3.9.17
< Location: https://aws.amazon.com/dynamodb/
<
* Connection #0 to host my-url-shortener-lb-new2-304039992.us-east-1.elb.amazonaws.com left intact

Notes

  • Congratulations on successfully setting up an AWS ECS cluster that runs a Docker container a Python URL shortener application. To ensure efficient load distribution, a Load Balancer has been deployed to handle the traffic.
  • Running this code will create AWS resources in your account that might not be included in the free tier.
  • Use this code at your own risk, I am not responsible for anything related to its use.

aws-url-shortener's People

Contributors

groorj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.