Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 29, 2023 06:58 pm GMT

Automate application deployment into Lightsail instance

In this post I am going to explain how easily we can deploy any application to Light-sail instance. The aim of this post is to explore Lightsail. We will do everything with bash using AWSCLI which can further be use in creating CICD pipeline.

Check here to know more about Lightsail - AWS Lightsail

Approach:

Image description

Prerequisite:

  • AWS account
  • IAM access and secret key with access to Lightsail and EMR.
  • AWS CLI
  • Docker

Steps 1: Configure AWSCLI in you local system.

You can follow the following steps if you are with me and using Ubuntu OS.

If you are on different OS, you can take help from here: AWSCLI

Step 2: Configure AWSCLI in your local

  • Open terminal and run aws configurecommand, it will ask access key and secrets key.
shubham@Shubham~ % aws configureAWS Access Key ID [****************aaaa]: aaaaaaaaaaaaaaaaaaaaaaAWS Secret Access Key [****************bbbb]: bbbbbbbbbbbbbbbbbbDefault region name [eu-central-1]: eu-central-1Default output format [None]: 

Step 3: Pull Grafana Image in your local system using the following command. You can also create your own Dockerfile as per your need.
docker pull grafana/grafana-enterprise

Step 4: Create ECR repo in AWS

  • Goto ECR console.
  • Click on create repo.
  • As a best practice you should select Visibility settings as private.
  • Give your repo name. In my case I have given lightsail as repo name.
  • Keep rest as default configuration.
  • Click on create and it will create a repo for you.

Step 5: Push grafana image to ECR.

  • Login to your ECR using the following commanddocker login -u AWS -p $(aws ecr get-login-password --region <region_name>) <aws_account_number>.dkr.ecr.<region_name>.amazonaws.com
  • Build your image with tag as lightsaildocker build -t lightsail .
  • After the build completes, tag your image so you can push the image to this repository:docker tag lightsail:latest <aws_account_number>.dkr.ecr.<region_name>.amazonaws.com/lightsail:latest
  • Run the following command to push this image to your newly created AWS repository:docker push <aws_account_number>.dkr.ecr.<region_name>.amazonaws.com/lightsail:latest

Now you pushed your Grafana image to ECR repo which we will pull from the instance and use for the deployment.

Step 6: This is most important step for our implementation. We need to create a sh file..thinking why? We need this file to pass some user data with Lightsail instance creation. We will see the content of this file one by one. This is most important step for the automation and as I said process will be fully automated and we are not going to do anything from console.
Create sh file with the following code
`

  1. sudo su
  2. apt update
  3. snap install docker
  4. cd /home/ubuntu
  5. curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
  6. apt install unzip -y
  7. unzip awscliv2.zip
  8. ./aws/install
  9. rm -rf awscliv2.zip
  10. export AWS_ACCESS_KEY_ID=
  11. export AWS_SECRET_ACCESS_KEY=
  12. docker login -u AWS -p $(aws ecr get-login-password --region ) .dkr.ecr.amazonaws.com
  13. docker pull .dkr.ecr..amazonaws.com/lightsail:latest
  14. docker run -d -p 3000:3000 250373516626.dkr.ecr.eu-central-1.amazonaws.com/lightsail:latest
  15. aws lightsail allocate-static-ip --static-ip-name
  16. aws lightsail attach-static-ip --static-ip-name --instance-name `

Explanation of the above code for better understanding:

line 1 - changing user as a sudo.
line 2 - Just simple update.
line 3 - Installing Docker in Lightsail.
line 6 to 10 - Installing AWSCLI in the Lightsail instance.
line 10 and 11 - This is not a best practice to pass IAM credential directly from here. The best approach will be pass it with environment variable or from the cred of your CI tool.
line 12 - login to ECR with ECR ecr-login-password
line 13 - Pulling grafana image from ECR repo that we pushed in earlier steps.
line 14 - Running docker container
line 15 - Create a static IP for your Lightsail instance
line 16 - attaching Static IP to our instance.

Step 7: Create Lightsail Instance using the following command:
aws lightsail create-instances --instance-names grfana --availability-zone eu-central-1a --blueprint-id ubuntu_22_04 --bundle-id nano_2_0 --user-data file://instance.sh

Explanation of the above command:
To create a lightsail instance 2 important parameter that we need to pass is: blueprint-id and bundle-id.
blueprint-id is basically the selection of platform, which application or which OS you want to configure in your instance. In my case I am going with ubuntu. You get check all the available value of blueprint-id by using the following command:
aws lightsail get-blueprints

And, bundle-id is the instance type for your Lightsail instance. You can get all available value of bundle by using the following command:
aws lightsail get-bundles

One important flaw is --user-data - here, we are instructing Lightsail to execute some sort of commands with instance creation. So, basically what ever script we mentioned in the above steps is going to be executed with your instance creation.

Step 8: Allow port on which your application is running to Lightsail to accept traffic on the given port. In my case it's 3000 port.
aws lightsail open-instance-public-ports --port-info fromPort=3000,toPort=3000,protocol=TCP --instance-name grfana

Step 9: [Optional] Attach DNS with your IP.

Step 10: Access you application from the browser with instance static IP or with DNS.


Original Link: https://dev.to/shubhamkcloud/automate-application-deployment-into-lightsail-instance-28co

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To