<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Benjamin Dorner on Medium]]></title>
        <description><![CDATA[Stories by Benjamin Dorner on Medium]]></description>
        <link>https://medium.com/@bedorn?source=rss-e60edb19d8cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-z1rx8qy0B65ggXqY-g4sw.png</url>
            <title>Stories by Benjamin Dorner on Medium</title>
            <link>https://medium.com/@bedorn?source=rss-e60edb19d8cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 11:52:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bedorn/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deployment of Strapi or any dockerized service on AWS Fargate]]></title>
            <link>https://medium.com/@bedorn/deployment-of-strapi-or-any-dockerized-service-on-aws-fargate-3e93da3a8513?source=rss-e60edb19d8cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e93da3a8513</guid>
            <category><![CDATA[strapi]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[aws-fargate]]></category>
            <category><![CDATA[elastic-container-service]]></category>
            <category><![CDATA[elastic-load-balancer]]></category>
            <dc:creator><![CDATA[Benjamin Dorner]]></dc:creator>
            <pubDate>Sat, 13 Apr 2024 11:12:02 GMT</pubDate>
            <atom:updated>2024-04-16T13:29:49.985Z</atom:updated>
            <content:encoded><![CDATA[<h3>Goal</h3><p>My goal was to deploy a strapi instance as a container in a cluster, including a fully automated deployment with Github actions and accessibility via https in a browser. Sounds very trivial, however it was not possible to find a decent guide to meet all those requirements. In my point of view, all components are mandatory for a high scalable web or mobile application. In case of strapi, there were several documentation that supported well, however the end to end configuration had to put together with various researches.</p><p>Why AWS Fargate?</p><p>AWS ECS with Fargate is a serverless option that abstracts the server management, allowing you to focus on the application itself. It’s suitable for containerized applications. Benefits are:</p><ul><li>No need to manage servers or clusters.</li><li>Pay only for the compute power you use.</li></ul><h3>Target Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*f7aGU75JgT_995uTFdf1Ig.png" /></figure><h3>Prerequisites</h3><p>Before proceeding, ensure you have:</p><ul><li>AWS account</li><li>Docker installed on your machine</li><li>Strapi project ready for deployment</li><li>Github repository</li><li>Domain with SSL Certificate (any Provider possible)</li></ul><h3>Setup postgres database</h3><p>You can use any other database, however in my case I use a potsgres database.</p><ul><li>DB engine Version: 14.11</li><li>Identifier: strapi</li><li>Credentials management: Self managed, and choose a master password that you have to use to connect to database</li><li>I chose a db.t3.micro instance configuration</li><li>Ipv4 mode and a security group has to be connected, in order to make the database accessible for the container:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QYSRFarZcakdE8yQMAeXMA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HCwM7hgdHQnoZAfJpp3Lcw.png" /></figure><p>Use a tool (e.g. pgAdmin) to connect to create database for strapi instance. To access the database, you have to set it as public accessible. After setting up the database, you can change it to keep it behind the doors, allowing connection within the VPC only.</p><h3>Containerizing Strapi with AWS ECS</h3><p>Containerizing Strapi with AWS ECS involves creating a Docker container for the Strapi application and deploying it to the AWS Elastic Container Service (ECS). This process allows for scalable and efficient management of Strapi instances in the cloud.</p><p>If you already have a locally running strapi project with docker, you can skip the docker compose part. You may also follow the official installation guide of Strapi here <a href="https://docs.strapi.io/dev-docs/installation/docker">https://docs.strapi.io/dev-docs/installation/docker</a>. In my case, the Strapi project is installed in a sub directory named src and I want to focus more on the deployment to AWS Fargate.</p><h4>Create Docker files</h4><p>Create <em>Dockerfile</em>, <em>Dockerfile.dev</em>, <em>docker-compose.yml</em> and <em>.dockerignore</em> in the root of your Strapi project with the necessary configurations. As said, here the Strapi project files are moved to a <em>./src</em> directory.</p><p><strong>docker-comspose.yml</strong></p><pre>version: &quot;3&quot;<br>services:<br>    strapi:<br>        container_name: strapi<br>        build:<br>            context: .<br>            dockerfile: Dockerfile.dev<br>        image: strapi:latest<br>        restart: unless-stopped<br>        env_file: .env<br>        environment:<br>            DATABASE_CLIENT: ${DATABASE_CLIENT}<br>            DATABASE_HOST: strapiDB<br>            DATABASE_PORT: ${DATABASE_PORT}<br>            DATABASE_NAME: ${DATABASE_NAME}<br>            DATABASE_USERNAME: ${DATABASE_USERNAME}<br>            DATABASE_PASSWORD: ${DATABASE_PASSWORD}<br>            JWT_SECRET: ${JWT_SECRET}<br>            ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}<br>            APP_KEYS: ${APP_KEYS}<br>            NODE_ENV: ${NODE_ENV}<br>        volumes:<br>            - ./src/config:/opt/app/config<br>            - ./src/src:/opt/app/src<br>            - ./src/package.json:/opt/package.json<br>            - ./src/yarn.lock:/opt/yarn.lock<br>            - ./.env:/opt/app/.env<br>            - ./src/public/uploads:/opt/app/public/uploads<br>        ports:<br>            - &quot;1337:1337&quot;<br>        networks:<br>            - strapi<br>        depends_on:<br>            - strapiDB<br>    strapiDB:<br>        container_name: strapiDB<br>        platform: linux/amd64 #for platform error on Apple M1 chips<br>        restart: unless-stopped<br>        env_file: .env<br>        image: postgres:12.0-alpine<br>        environment:<br>            POSTGRES_USER: ${DATABASE_USERNAME}<br>            POSTGRES_PASSWORD: ${DATABASE_PASSWORD}<br>            POSTGRES_DB: ${DATABASE_NAME}<br>        volumes:<br>            - strapi-data:/var/lib/postgresql/data/ #using a volume<br>            #- ./src/data:/var/lib/postgresql/data/ # if you want to use a bind folder<br>        ports:<br>            - &quot;5432:5432&quot;<br>        networks:<br>            - strapi<br>volumes:<br>    strapi-data:<br>networks:<br>    strapi:<br>        name: Strapi<br>        driver: bridge</pre><p><strong>Dockerfile.dev</strong></p><pre>FROM node:18-alpine<br># Installing libvips-dev for sharp Compatibility<br>RUN apk update &amp;&amp; apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev git<br>ARG NODE_ENV=development<br>ENV NODE_ENV=${NODE_ENV}<br><br>WORKDIR /opt/<br>COPY ./src/package.json ./src/package-lock.json ./<br>RUN npm install -g node-gyp<br>RUN npm config set fetch-retry-maxtimeout 600000 -g &amp;&amp; npm install<br>ENV PATH /opt/node_modules/.bin:$PATH<br><br>WORKDIR /opt/app<br>COPY ./src/ .<br>RUN chown -R node:node /opt/app<br>USER node<br>RUN [&quot;npm&quot;, &quot;run&quot;, &quot;build&quot;]<br>EXPOSE 1337<br>CMD [&quot;npm&quot;, &quot;run&quot;, &quot;develop&quot;]</pre><p><strong>Dockerfile</strong></p><pre>FROM node:18-alpine as build<br>RUN apk update &amp;&amp; apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git &gt; /dev/null 2&gt;&amp;1<br>ARG NODE_ENV=production<br>ENV NODE_ENV=${NODE_ENV}<br><br>WORKDIR /opt/<br>COPY ./src/package.json ./src/package-lock.json ./<br>RUN npm install -g node-gyp<br>RUN npm config set fetch-retry-maxtimeout 600000 -g &amp;&amp; npm install --only=production<br>ENV PATH /opt/node_modules/.bin:$PATH<br>WORKDIR /opt/app<br>COPY ./src/ .<br>RUN npm run build<br><br># Creating final production image<br>FROM node:18-alpine<br>RUN apk add --no-cache vips-dev<br>ARG NODE_ENV=production<br>ENV NODE_ENV=${NODE_ENV}<br>WORKDIR /opt/<br>COPY --from=build /opt/node_modules ./node_modules<br>WORKDIR /opt/app<br>COPY --from=build /opt/app ./<br>ENV PATH /opt/node_modules/.bin:$PATH<br><br>RUN chown -R node:node /opt/app<br>USER node<br>EXPOSE 1337<br>CMD [&quot;npm&quot;, &quot;run&quot;, &quot;start&quot;]</pre><p><strong>.dockerignore</strong></p><pre>.tmp/<br>.cache/<br>.git/<br>.env<br>build/<br>src/node_modules/<br>node_modules/<br><br># Ignoring folders that might be used in starter templates<br><br>data/<br>backup/</pre><h4>Create IAM role for github action</h4><p>You need to grant github acces to ESC operations. Create a role, for instance <em>github-actions-role</em> and add certain policies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G_r99bhuw61PwgNB5cPdlg.png" /></figure><h4>Create repository in Elastic Container Registry</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*OuMy_68-lPnLeh9Q25eFOw.png" /></figure><p>Here you see the<em> Image URI</em> of the image that you have to connect later to the container in the task definition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Njerc7pcTgGDge1k8dRE7Q.png" /></figure><h4>Setup github workflow</h4><p>The following example workflow demonstrates how to build a container image and push it to Amazon ECR. It then updates the task definition with the new image ID, and deploys the task definition to Amazon ECS.</p><p>Ensure that you provide your own values for all the variables in the <em>env</em> key of the workflow after you have successfully done the next steps.</p><p>If you configured a deployment <em>environment</em>, change the value of environment to be the name of your <em>environment</em>. If you did not configure an environment or if your workflow is in a private repository and you do not use GitHub Enterprise Cloud, delete the environment key.</p><p>Add the arn of the <em>github-actions-role</em> which you have defined in the previous step to the role-to-assume key.</p><p>Add the name of the <em>ECR repository</em> which you have defined in the previous step to the ECS_CLUSTER key in env property.</p><p>The missing ECS vars (Service and Cluster) will be added later, when the service has been deployed for the first time.</p><pre># This workflow will build and push a new container image to Amazon ECR,<br># and then will deploy a new task definition to Amazon ECS, when there is a push to the &quot;main&quot; branch.<br><br>name: Deploy to Amazon ECS<br><br>on:<br>    push:<br>        branches:<br>            - &quot;main&quot;<br><br>env:<br>    AWS_REGION: eu-central-1<br>    ECR_REPOSITORY: strapi<br>    ECS_SERVICE: strapi-service<br>    ECS_CLUSTER: strapi-cluster<br>    ECS_TASK_DEFINITION: .github/workflows/task-definition.json<br>    CONTAINER_NAME: strapi-container<br><br>permissions:<br>    id-token: write<br>    contents: read # This is required for actions/checkout@v2<br><br>jobs:<br>    deploy:<br>        name: Deploy<br>        runs-on: ubuntu-latest<br>        environment: production<br><br>        steps:<br>            - name: Checkout<br>              uses: actions/checkout@v3<br>            - name: Configure AWS credentials<br>              uses: aws-actions/configure-aws-credentials@v1<br>              with:<br>                  role-to-assume: arn:aws:....../github-actions-role<br>                  role-session-name: Github<br>                  aws-region: ${{ env.AWS_REGION }}<br><br>            - name: Login to Amazon ECR<br>              id: login-ecr<br>              uses: aws-actions/amazon-ecr-login@v1<br><br>            - name: Build, tag, and push image to Amazon ECR<br>              id: build-image<br>              env:<br>                  ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}<br>                  IMAGE_TAG: ${{ github.sha }}<br>              run: |<br>                  # Build a docker container and<br>                  # push it to ECR so that it can<br>                  # be deployed to ECS.<br>                  docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./<br>                  docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG<br>                  echo &quot;::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG&quot;<br>            - name: Fill in the new image ID in the Amazon ECS task definition<br>              id: task-def<br>              uses: aws-actions/amazon-ecs-render-task-definition@v1<br>              with:<br>                  task-definition: ${{ env.ECS_TASK_DEFINITION }}<br>                  container-name: ${{ env.CONTAINER_NAME }}<br>                  image: ${{ steps.build-image.outputs.image }}<br><br>            - name: Deploy Amazon ECS task definition<br>              uses: aws-actions/amazon-ecs-deploy-task-definition@v1<br>              with:<br>                  task-definition: ${{ steps.task-def.outputs.task-definition }}<br>                  service: ${{ env.ECS_SERVICE }}<br>                  cluster: ${{ env.ECS_CLUSTER }}<br>                  wait-for-service-stability: true</pre><h3>Setup Security Groups</h3><p>In order to have a network communication between the service (container), task and Loadbalancer, you need to define security groups. One Group for outbound traffic and one for inbound traffic between the Loadbalancer and Container. It is very important to always chose the correct VPC, which is mostly set as default.</p><ul><li>Open EC2 Console and select <em>Security Groups</em> in the left menu below the <em>Network &amp; Security</em> header.</li><li>Create Security Group</li><li>Name it <em>ApplicationLoadBalancerSecurityGroup</em>, do not change outbound rule and create inbound rule as following:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TNvISPY35qvHDlz_Uf9WRg.png" /></figure><p>Create a second rule, name it <em>ContainerFromALBSecurityGroup and set the following rules.</em></p><p>Note: the source for the inbound rule is the security you have created (<em>ApplicationLoadBalancerSecurityGroup)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gpQwpYs8ChzBdb7LauKPXQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AbAF-iJpfmz68H6RO6Vjig.png" /></figure><h3>Setting up AWS ECS</h3><h4>Store .env file in S3</h4><p>There is way to store env vars iwith the AWS Secret Manager, however for a usage within a javascript project it requires a library to access vars. For that reason I chose to store env vars in a S3 Bucket.</p><p>Create private S3 Bucket (turn on to block all public access) and Upload .env file. Example:</p><pre>HOST=0.0.0.0<br>PORT=1337<br>APP_KEYS=<br>API_TOKEN_SALT=<br>ADMIN_JWT_SECRET=<br>TRANSFER_TOKEN_SALT=<br># Database<br>DATABASE_CLIENT=postgres<br>DATABASE_HOST=127.0.0.1<br>DATABASE_PORT=5432<br>DATABASE_NAME=strapi<br>DATABASE_USERNAME=strapi<br>DATABASE_PASSWORD=strapi<br># DATABASE_SSL=false<br><br>NODE_ENV=production<br>JWT_SECRET=</pre><p>Add permission to github-actions-role (or any policy attached) in <em>Statement</em> to access S3 Bucket:</p><pre>{<br>    &quot;Effect&quot;: &quot;Allow&quot;,<br>    &quot;Action&quot;: [<br>        &quot;s3:GetObject&quot;<br>    ],<br>    &quot;Resource&quot;: &quot;arn:aws:s3:::ec2imagebuilder*&quot;<br>},<br>{<br>    &quot;Effect&quot;: &quot;Allow&quot;,<br>    &quot;Action&quot;: [<br>        &quot;logs:CreateLogStream&quot;,<br>        &quot;logs:CreateLogGroup&quot;,<br>        &quot;logs:PutLogEvents&quot;<br>    ],<br>    &quot;Resource&quot;: &quot;arn:aws:logs:*:*:log-group:/aws/imagebuilder/*&quot;<br>}</pre><h4>Configure an ECS cluster</h4><p>Enter a name for the cluster and select <em>Fargate (serverless)</em> as Infrastructure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ygc4ljRByBV-zXGcBXOSoQ.png" /></figure><p>Copy the cluster name to your env vars in the <em>aws.yml</em> file located in the <em>.github/workflows</em> directory.</p><h4>Define an ECS task definition</h4><p>Go to ECS (Elastic Container Service) Service Console, choose <em>Task definitions</em> in the left menu and create new task definition.</p><p>Enter a name and choose infrastructure options as needed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5amklVDRF0G1cceYAQAL0w.png" /></figure><p>Enter the <em>Image URI</em> that you have registered in the ECR Console. As my strapi instance within the container runs on PORT 1337, I have to map it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vd9KQrCWjgVV1kSAa-p7pA.png" /></figure><p>Add S3 <em>ARN</em> as location for .env file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TsJdcPv4yfU0S-BHq3neIA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pk79mv_7oOcnEOLpAoogjg.png" /></figure><p>Click create, create a task-definition.json file in your .github/workflows directory. Copy the task definition as json to the local file and save it.</p><h4>Create Service in ECS Cluster</h4><p>Go to cluster page, click your created cluster name and under the <em>Services tab</em> click create. Select <em>launch type Fargate</em> with latest platform version</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*m_Jxi8-fPJ2HvxdK-HnQ6g.png" /></figure><p>If there is no revision of the task definition, you have to deploy a new revision first in order to proceed. Choose <em>application type Service</em>, your created <em>task definition family</em> and the last revision of it. Give it a service name and add the name to the <em>aws.yml</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*zC77D2bOOGkRfiw6HzRqGg.png" /></figure><p>Open the Network category and choose the security group <em>ContainerFromALBSecurityGroup</em> you have created in a previous step. Turn on public IP also.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*STevmXgNno4b4riFv89xAg.png" /></figure><p>Open the Load Balancing bar and select the following options:</p><ul><li>Load balancer type: Application Load Balancer</li><li>Container: strapi-container 1337:1337</li><li>Create new load balancer</li><li>Give it a name like strapi-loadbalancer</li></ul><p>Add Listener</p><ul><li>Port: 443</li><li>Protocol: HTTPS</li><li>Choose Certificate from ACM or IAM</li></ul><p>Create new target target group</p><ul><li>Give it name like strapi-target-group</li><li>Protocol HTTP</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wp69d7-mfpS6f35-1QqH-Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mnXEwArp1Jl5qdCACKqPBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7zAI4sD3gg-ylmh39Xp-A.png" /></figure><h4>Load Balancer changes</h4><p>IMPORTANT: Change security group for load balancer</p><p>Go to EC2 Console and choose <em>Load Balancers</em> below the Load Balancing category in the menu and select the created load balancer. Within the tab security you have to edit the security group. Select <em>ApplicationLoadBalancerSecurityGroup</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*ui1IZCfdX4po370ty7Qo8A.png" /></figure><p>OPTIONAL: Redirect Request to Port 80 to 443. Add Listener with the following rule to Load Balancer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*wKeux1pECcC1hIoTuS5zXA.png" /></figure><p>How the listeners have to be setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*eXbu0h8WGTb64IIA2zK9KQ.png" /></figure><h3>Setup Domain, routing and SSL</h3><p>In my case, the domain was registered in AWS and I use a subdomain for my strapi instance. However you can use domains of any other provider. It may happen that there are other ways to verify the ownerhsip.</p><ol><li>Create a hosted zone with your domain and add NS entry in order to verify the ownership of the domain.</li><li>In my case the SSL Cert is also registered in AWS (Certificate Manager) and the verification is done by adding a CNAME to the hosted zone.</li><li>Define an A-Record and route it to the dns name of the loadbalancer, in my example</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G60fp5NqgFbx-K7WpNSpIA.png" /></figure><h3>Links</h3><p><a href="https://strapi.io/">https://strapi.io/</a></p><p><a href="https://www.restack.io/docs/strapi-knowledge-strapi-deployment-aws-guide">https://www.restack.io/docs/strapi-knowledge-strapi-deployment-aws-guide</a></p><p><a href="https://docs.github.com/en/actions/deployment/deploying-to-your-cloud-provider/deploying-to-amazon-elastic-container-service">https://docs.github.com/en/actions/deployment/deploying-to-your-cloud-provider/deploying-to-amazon-elastic-container-service</a></p><p><a href="https://www.youtube.com/watch?v=rUgZNXKbsrY">https://www.youtube.com/watch?v=rUgZNXKbsrY</a></p><p><a href="https://www.youtube.com/watch?v=JQP96EjRM98">https://www.youtube.com/watch?v=JQP96EjRM98</a></p><p><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/use-environment-file.html">https://docs.aws.amazon.com/AmazonECS/latest/developerguide/use-environment-file.html</a></p><h3>Tags</h3><p>#strapi #github #githubactins #aws #route53 #fargate #docker #ecs #ecr #elasticloadbalancer #rds</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e93da3a8513" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>