{"id":27878978,"url":"https://github.com/sajonaro/ecs-ec2-ref","last_synced_at":"2025-08-17T16:53:54.496Z","repository":{"id":243568965,"uuid":"808822757","full_name":"sajonaro/ecs-ec2-ref","owner":"sajonaro","description":"aws ecs based inftrastructure built with terraform, blue-green deployment included","archived":false,"fork":false,"pushed_at":"2024-06-24T14:40:17.000Z","size":86,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-05T03:16:51.036Z","etag":null,"topics":["blue-green-deployment","code-deploy","ec2","ecs","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sajonaro.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T22:41:45.000Z","updated_at":"2024-07-10T17:10:50.000Z","dependencies_parsed_at":"2024-06-23T15:43:48.091Z","dependency_job_id":"2a64f2ef-ae6e-4755-a824-1b4c67b68d63","html_url":"https://github.com/sajonaro/ecs-ec2-ref","commit_stats":null,"previous_names":["sajonaro/ecs-ec2-ref"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sajonaro%2Fecs-ec2-ref","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sajonaro%2Fecs-ec2-ref/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sajonaro%2Fecs-ec2-ref/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sajonaro%2Fecs-ec2-ref/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sajonaro","download_url":"https://codeload.github.com/sajonaro/ecs-ec2-ref/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252429968,"owners_count":21746574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blue-green-deployment","code-deploy","ec2","ecs","terraform"],"created_at":"2025-05-05T03:16:54.838Z","updated_at":"2025-05-05T03:16:55.335Z","avatar_url":"https://github.com/sajonaro.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"## What? \n\nGOAL 1 \u003cbr\u003e of this PoC is: to explore options of containerized applications to use container volumes to read/write files to/from common share (s3 ).\nThis can be useful when we want to scale out a service dealing with files (e.g. a CMS)\n\nGOAL 2 \u003cbr\u003e is to provide a working example of ECS with EC2 launch type setup, as it requires a bit more configuration than the launch type FARGATE, so to have a working and complete example comes handy     \n\nProvided code:\n- builds a hello world app's docker image and uploads to ECR\n- defines ECS cluster with launch type EC2:\n   - task definition using application image from step above\n   - capacity provider configuring container instances (aka EC2s ) parameters\n   - docker volume with driver = local (for local file system) bound to container's local directory \n \n## TL/DR;\n\nWe make our application \"think\" it accesses just a local directory, whereas it is actually bound to an s3 bucket behind the scenes \n\nImplementation details:\n- given existing bucket (created outside current terraform stack) we provide access to it as follows: create IAM role, assign it to EC2 instances in the cluster,give this role permissions to access s3, and provide resource policy for s3 to allow access by the IAM role. \n- We use mount-s3 utility (https://aws.amazon.com/about-aws/whats-new/2023/03/mountpoint-amazon-s3/) to mount EC2's local folder to external s3 bucket using user_data.sh initializing script, mount-s3 utility uses policies attached to EC2s IAM role to access specified bucket \n- In task definition we use volume configuration to bind host's folder (with we mounted in step above) to container's local folder.\n\n\n\n\n## How to build \u0026 run?\n\n- provide .env file with valid AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION values ( for docker compose to pick it up)\n- build application's image \u0026 push it to ECR:\n    \n    ```           \n    ./build_push_to_ecr.sh\n    ```  \n- provide terraform.tfvars file with : IMAGE_URL (update this value based on step above), S3_BUCKET_NAME (see description in vars.tf file)\n\n- generate ssh key pair (tf-key), these keys are used to ssh into EC2s via bastion host: tf-key.pub should be replaced in variable public_ec2_key, e.g. use ssh-keygen :\n    ```\n    ssh-keygen\n    ```  \n      \n- use usual terraform commands to deploy ECS stack (albeit via docker compose to avoid cluttering local environment \u0026 making sure we use fixed tf version)\n   \n    ```\n    docker compose run --rm tf init\n    docker compose run --rm tf validate \n    docker compose run --rm tf plan \n    docker compose run --rm tf apply  --auto-approve \n\n    ```\n- Finally, access deployed application via browser using ALBs DNS name (use output value from terraform [application_url], or login into AWS console) e.g.:\n```\nhttps://hello-world-app-alb-1233026957.eu-central-1.elb.amazonaws.com\n``` \n- Alternatively (access via dns name) e.g.:\n```\nhttps://dev-ops.pro   \n```\n\n   \n\n## 101 on using ssh agent \n\n- make sure to start ssh agent (on your local host)\n```\neval $(ssh-agent -s) \u003e /dev/null\n```  \n- register your private key in ssh agent:\n```\nssh-add path_to/your_private_key\n``` \n- you can now enjoy using ssh forwarding:\n\n    ```\n    ssh -A ec2@bastion_host_ip\n    ```\n    then, from bastion host (N.B. no need to provide -i/upload private key):\n    ```\n    ssh ec2-user@ec2_private_ip\n    ```\n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajonaro%2Fecs-ec2-ref","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsajonaro%2Fecs-ec2-ref","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajonaro%2Fecs-ec2-ref/lists"}