Skip to content

feat: deploy to aws #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .ebextensions/00_rds_env.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
files:
"/tmp/set_env.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
DB_USERNAME="$(/opt/elasticbeanstalk/bin/get-config environment -k RDS_USERNAME)"
DB_PASSWORD="$(/opt/elasticbeanstalk/bin/get-config environment -k RDS_PASSWORD)"
DB_HOST="$(/opt/elasticbeanstalk/bin/get-config environment -k RDS_HOSTNAME)"
DB_NAME="$(/opt/elasticbeanstalk/bin/get-config environment -k RDS_DB_NAME)"

export DATABASE_URL="postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}"
echo "DATABASE_URL=${DATABASE_URL}" >> /opt/elasticbeanstalk/deployment/env.list

commands:
00-set-env:
command: /tmp/set_env.sh
19 changes: 19 additions & 0 deletions .ebextensions/01_app_release.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/01_app_release.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
if [ -f /tmp/leader_only ]; then
echo "Running Migrations"
container_id=`docker ps -q --no-trunc --filter label="com.amazonaws.ecs.container-name=" | head -n 1`
docker inspect $container_id
docker exec $container_id /entrypoint.sh npx prisma migrate deploy
fi
container_commands:
01_touch_the_leader:
command: |
#!/usr/bin/env bash
touch /tmp/leader_only
leader_only: true
17 changes: 17 additions & 0 deletions .ebextensions/log.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
files:
"/etc/awslogs/config/beanstalklogs.conf":
mode: "000644"
user: root
group: root
content: |
[/var/log/eb-docker/containers/eb-current-app/stdouterr.log]
log_group_name=/aws/elasticbeanstalk/EB-ENV-NAME/var/log/eb-docker/containers/eb-current-app/stdouterr.log
log_stream_name={instance_id}
file=/var/log/eb-docker/containers/eb-current-app/*stdouterr.log
file_fingerprint_lines=1-20

commands:
01_remove_eb_stream_config:
command: 'rm /etc/awslogs/config/beanstalklogs.conf.bak'
02_restart_log_agent:
command: 'service awslogs restart'
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,14 @@ lerna-debug.log*
.env
.env.dist
.env.test
Dockerrun.aws.json
etc/infra/aws/.terraform
etc/infra/aws/Dockerrun.aws.json
etc/infra/aws/Dockerrun.aws.zip
Dockerrun.aws.zip
*.tfvars

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ COPY --chown=carbonable:carbonable --from=builder /srv/www/prisma ./prisma
EXPOSE 8080

CMD ["npm", "run", "start:prod"]

FROM node:20-alpine3.17 as aws_runtime

WORKDIR /srv/www

COPY --from=builder /srv/www/package.json ./package.json
COPY --from=builder /srv/www/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /srv/www/node_modules ./node_modules
COPY --from=builder /srv/www/dist ./dist
COPY --from=builder /srv/www/src/schemas ./src/schemas
COPY --from=builder /srv/www/prisma ./prisma
COPY .ebextensions ./.ebextensions

EXPOSE 8080

CMD ["npm", "run", "start:prod"]
5 changes: 5 additions & 0 deletions etc/infra/aws/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
126 changes: 126 additions & 0 deletions etc/infra/aws/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added etc/infra/aws/Dockerrun.aws.zip
Binary file not shown.
78 changes: 78 additions & 0 deletions etc/infra/aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# CCRP - Infrastructure as Code

[Getting Started with Terraform & AWS](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

First you need to install :

- [ ] Terraform
- [ ] AWS CLI

Connect to [AWS IAM console](https://us-east-1.console.aws.amazon.com/iam/home?region=eu-west-3#/home) and create an account for cli usage named `carbonable_deployer`
Connect to [Github.com](https://github.com/settings/tokens) and create a new access token **with admin:hooks** roles(required to create amplify app for frontend to work) **TOKEN HAS TO BE CREATED BY ORG ADMIN**
Add this token to the `.tfvars` file

Then under `Users > Security Credentials` create new access key and configure them with the access key id and secret access key they give you.

```bash
aws configure --profile carbonable_deployer
```

Create the backend bucket :

```bash
aws s3api create-bucket --bucket carbonable-deployments-backends --region eu-west-3 --profile carbonable_deployer
```

Init backend :

```bash
AWS_PROFILE=carbonable_deployer terraform init -backend-config=${client}.backend-config.tfvars
```

Update backend : (when you changes infrastructure files)

```bash
AWS_PROFILE=carbonable_deployer terraform plan -var-file=${client}.tfvars
AWS_PROFILE=carbonable_deployer terraform apply -var-file=${client}.tfvars
```

When applying changes to `src` directory will trigger a new build of docker version. Applying terraform changes will subsequently trigger a new deploy of the application.

```bash
AWS_PROFILE=carbonable_deployer terraform apply -var-file=${client}.tfvars
```

### Logging

Application logs are available in the AWS CloudWatch console under `/aws/elasticbeanstalk/{client}-{env}-eb-environment/var/log/eb-docker/containers/eb-current-app/stdouterr.log` log group
Also keep in mind that you can access all the logs of the deployment / container creation under `Elastic Beanstalk > Environment > Logs > Request logs (full)` and download the bundle

### Considerations

When updating infrastructure configuration files, you may need to update zip files by hand, as this may not be taken in account in the apply command.
For further configuration of the Elastic Beanstalk environment you may want to read the following section.

### Extend AWS Elastic Beanstalk environment

Check informations described here : [https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html)

- <https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-cfg-softwaresettings.html>
- <https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker.container.console.html>
- <https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html>
- <https://github.com/awsdocs/elastic-beanstalk-samples/tree/main/configuration-files>

### AWS EB cli

Install eb cli :

```bash
brew install awsebcli
```

Init env :

```bash
eb init
```

and follow the instructions
40 changes: 40 additions & 0 deletions etc/infra/aws/amplify.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource "aws_amplify_app" "frontend" {
name = "${var.app_name}-frontend"
repository = "https://github.com/carbonable/ccrp-front"
access_token = var.github_access_token

build_spec = <<-EOT
version: 0.1
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
EOT

environment_variables = {
BACKEND_URL = aws_elastic_beanstalk_environment.eb_environment.endpoint_url
}
}

resource "aws_amplify_branch" "main" {
app_id = aws_amplify_app.frontend.id
branch_name = "main"
stage = "PRODUCTION"
}

resource "aws_amplify_webhook" "main_trigger" {
app_id = aws_amplify_app.frontend.id
branch_name = aws_amplify_branch.main.branch_name
description = "triggermaster"
}
Loading