Skip to content

Latest commit

 

History

History
162 lines (126 loc) · 4.5 KB

README.md

File metadata and controls

162 lines (126 loc) · 4.5 KB

Elastic Beanstalk Deployment

Table of Contents

React-ts-Deployment

Create Dockerfile in the root of the project

name: Deploy to Elastic Beanstalk
on:
  push:
    branches:
      - main
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.<ENV>_AWS_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.<ENV>_AWS_SECRET_KEY }}
          aws-region: <REGION>

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
      - name: Build, tag, and push image to Amazon ECR
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: <REPOSITORY>
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build \
          -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

      - name: Prepare Dockerrun file
        run: |
          export IMAGE_TAG=${{ github.sha }}
          export APP_NAME=<App-Name>
          sed -i 's/<app-name>/'"$APP_NAME"'/g' Dockerrun.aws.json
          sed -i 's/<version-tag>/'"$IMAGE_TAG"'/g' Dockerrun.aws.json
          zip -r deploy.zip Dockerrun.aws.json .platform/

      - name: Deploy to Elastic Beanstalk
        uses: einaregilsson/beanstalk-deploy@v18
        with:
          aws_access_key: ${{ secrets.<ENV>_AWS_ACCESS_KEY }}
          aws_secret_key: ${{ secrets.<ENV>_AWS_SECRET_KEY }}
          application_name: <Application-Name>
          environment_name: <Environment-Name>
          existing_bucket_name: <S3-Bucket-Name>
          region: <REGION>
          version_label: ${{ github.sha }}
          deployment_package: deploy.zip

Create Dockerrun.aws.json in the root of the project

{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "<STRING>.dkr.ecr.<REGION>.amazonaws.com/<app-name>:<version-tag>",
    "Update": "true"
  },
  "Ports": [{
    "ContainerPort": "80"
  }]
}

Configure EC2 Role

  • In AWS IAM servıce create a role with AWS Service in Trusted entities and EC2 in Use case
  • Attach these policies to the role
    • AWSElasticBeanstalkWorkerTier
    • AWSElasticBeanstalkWebTier
    • AWSElasticBeanstalkMulticontainerDocker
  • If you use ECR for deployment, attach AmazonEC2ContainerRegistryReadOnly policy
  • Set the role name as aws-elasticbeanstalk-ec2-role-<applicationName>-react-<environmentName>

Configure IAM User

  • In AWS IAM service create a user and attach AdministratorAccess-AWSElasticBeanstalk policy
  • If you user ECR attach these policies as well AmazonEC2ContainerRegistryPowerUser, AmazonEC2ContainerRegistryReadOnly and AmazonEC2ContainerServiceEventsRole
  • In Security Credentials of created user create an access key and secret key using CLI
  • Add these keys to GitHub repository secrets as <ENV>_AWS_ACCESS_KEY and <ENV>_AWS_SECRET_KEY

Create Elastic Beanstalk Application

Environment tier
  • Select Web server environment

Application Information

  • Enter application name: <application-name>-React

Environment information

  • Enter environment name: <environment-name>-React

Domain Name

  • You can use the default domain name or create a new one

Platform

  • Select Docker
  • Select Docker running on 64bit Amazon Linux 2
  • Platform version: select the latest version

Presets

  • Select Single instance (free tier)

  • Go to Next Page

Service access

  • Select Create and use new service role in Service role
  • Select the role created in the previous step

Skip to review

  • Review the configuration and click Create environment

Configure-Nginx-Redirection-For-React-App

Create nginx.conf in the root of the project

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Add the following lines to Dockerfile before EXPOSE 80

COPY nginx.conf /etc/nginx/conf.d/default.conf