{"id":21765810,"url":"https://github.com/sharinas/aws-copilot-containerized-flask-app","last_synced_at":"2026-05-03T01:39:59.886Z","repository":{"id":48365128,"uuid":"386108845","full_name":"SharinaS/aws-copilot-containerized-flask-app","owner":"SharinaS","description":"Infrastructure as Code to use AWS Copilot to (1) containerize with Fargate and run a simple Python Flask app and (2) automate deployments with AWS CodePipeline.","archived":false,"fork":false,"pushed_at":"2021-07-29T23:03:44.000Z","size":530,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-21T10:40:03.007Z","etag":null,"topics":["aws","cicd","copilot","docker","ecs","fargate","flask","pipeline","python"],"latest_commit_sha":null,"homepage":"","language":"Dockerfile","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/SharinaS.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}},"created_at":"2021-07-15T00:04:28.000Z","updated_at":"2025-02-14T17:46:21.000Z","dependencies_parsed_at":"2022-08-29T17:12:09.909Z","dependency_job_id":null,"html_url":"https://github.com/SharinaS/aws-copilot-containerized-flask-app","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SharinaS/aws-copilot-containerized-flask-app","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SharinaS%2Faws-copilot-containerized-flask-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SharinaS%2Faws-copilot-containerized-flask-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SharinaS%2Faws-copilot-containerized-flask-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SharinaS%2Faws-copilot-containerized-flask-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SharinaS","download_url":"https://codeload.github.com/SharinaS/aws-copilot-containerized-flask-app/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SharinaS%2Faws-copilot-containerized-flask-app/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32555839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T00:31:16.350Z","status":"ssl_error","status_checked_at":"2026-05-03T00:31:15.546Z","response_time":132,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["aws","cicd","copilot","docker","ecs","fargate","flask","pipeline","python"],"created_at":"2024-11-26T13:14:03.354Z","updated_at":"2026-05-03T01:39:59.854Z","avatar_url":"https://github.com/SharinaS.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ECS Copilot Flask App\n\nThis repo explores using AWS Copilot to deploy a simple Flask app. It additionally automates development with the a CodePipeline set up by Copilot.\n\n## A Foundational Hello World App\n\nThis section outlines steps to deploy a very basic app to get one's feet wet with copilot.\n\nSteps to deploy a first hello world app are here in the [copilot getting started documentation](https://aws.github.io/copilot-cli/docs/getting-started/first-app-tutorial/).\n\nDefault setup using `copilot init` can create a dockerfile for you. The following setup lets the app be accessible from the web.\n\n![screenshot of terminal](images/initial-setup.png)\n\nA number of resources are deployed to support ECS during the init-stimulated setup:\n\n![screenshot of terminal](images/infrastructure.png)\n\nThe app deployed is a simple static website - frontend only.\n\n## Custom Hello World with Flask\n\nA hello-world Flask app is containerized to explore the options and possiblities with Copilot - everything here on out is based on this Flask app.\n\nA virtual environment was set up, using `pipenv shell` (use `exit` to get out of the virtual env't, and `pipenv --rm` to remove the packages but keep the code, and `psh` to launch a subshell in the vitual environment after exiting.)\n\nFlask was installed with `pip install Flask`.\n\nRequirements file created with `pipenv run pip freeze \u003e requirements.txt` (while virtual environment running).\n\nTested the app locally:\n\n```bash\nexport FLASK_APP=app.py\npython -m flask run\n```\n\nDeployed the app with `copilot init` and updated it with `copilot svc deploy` when changes were made in files.\n\nAbout ports and exposing them:\n\n* https://docs.docker.com/language/python/run-containers/\n\n### Flask and Docker Documentation\n\n* Flask quickstart - https://flask.palletsprojects.com/en/1.1.x/quickstart/\n* Docker and Flask - https://docs.docker.com/language/python/build-images/\n\n## Deployment Options\n\nYou can either build everything from scratch, including a VPC, or you can build your ECS-related resources and add them to an existing VPC.\n\n### Create All New Resources\n\nDeploy using the terminal, with the command `copilot init`, and follow the prompts.\n\nOr, run the following command, which assumes knowledge of what is desired:\n\n```bash\ncopilot init --app [name-of-the-application] \\\n  --name [name-of-the-service-or-job] \\\n  --type \"Load Balanced Web Service\" \\\n  --dockerfile \"./Dockerfile\" \\\n  --deploy\n  ```\n\n### Deploy Into An Already existing VPC\n\nRun a series of commands one after another to get very specific about the configuration you want:\n\nRun `copilot app init`, which created copilot/workspace file, and deployed a couple IAM roles into AWS.\n\nRun `copilot env init` to create a name for the environment, ie test. It gives the option to create a new VPC etc, or import an existing VPC and subnets. It provides the VPC names from the account to choose from. A stackset is then deployed to AWS, as well as a cloudformation template with a cluster, IAM and security group resources, etc.\n\nRun `copilot svc init` to create the manifest file and tell it where the docker file is. This is the command that creates a new service to run your code. It will also create an ECR repository for the service. Then, run `copilot svc deploy`.\n\nYou'll have one stackset and three stacks deployed in your CloudFormation console, and your command line should produce a URL to view your app.\n\n## Add AWS CodePipeline\n\nCopilot can build an AWS CodePipeline for your app. I used GitHub for version control.\n\nMake sure all the code thus far is nicely stored in a GitHub repo. Run `copilot pipeline init`, which will create a pipeline yaml file and a buildspec file for the pipeline. Then, push the updated code changes to the repo.\n\nTo deploy the resources into AWS and be prompted to set up a CodeStar Connection (unless you already added a connection to your pipeline file), run `copilot pipeline update` to build the pipeline. When you create a connection when prompted, if you need to delete a pipeline with copilot, the connection will also be conveniently deleted.\n\n### Delete the Pipeline\n\nRemove all the resources associated with the pipeline with `copilot pipeline delete`.\n\n### Scale the Tasks\n\nTo change the number of tasks running, open the manifest file found in the `copilot/stubbs-frontend` directory. The manifest file is a declarative yal template that includes docker image, load balancer requirements, environment variables and resource allocation. Data is dynamically populated from the Dockerfile and from default values.\n\nChange the number of tasks, then run `copilot svc deploy`.\nConfirm the deploy with `copilot svc status -n stubbs-frontend`\n\n## Quick Reference of Copilot Commands\n\nTo build it all, use `copilot init`.\n\nTo update service and resources, `copilot deploy`.\n\nGet Service summary: `copilot svc show`\n\nList of AWS resources associated with service: `copilot svc show --resources`\n\nCheck service status: `copilot svc status`\n\nCheck service logs: `copilot svc logs`\n\nGet a list of options with `copilot app`.\n\nList the app with `copilot app ls`. Then, plug in the name of the app you found into `copilot app show`.\n\n### Examine the Environment\n\nGet a list of commands with `copilot env`.\n\n### Interact with the Front End Service\n\nList out the commands available with `copilot svc`.\n\n`copilot svc package` produces the CFn template for the service deployment. You'd need to do this if you wanted to start managing CFn deployments manually.\n\n`copilot svc deploy` is the command to use when you need to locally push service changes up to the chosen environment. Once you have a pipeline setup, use the CI/CD workflow instead. If you update the manifest file, this is the command you use to deploy the changes to the cloud environment.\n\n`copilot svc status` provides health and task info, and task count.\n\n### Delete the App\n\nDelete the AWS resources deployed in AWS (all those created from copilot init): `copilot app delete`\n\n## Observations\n\n### VPC CIDR\n\nCopilot init will deploy a VPC before it sets up ECS if default settings are chosen. It will use CIDR 10.0.0.0/16, and if there is another VPC in the account that uses the same, it will use the CIDR regardless.\n\n### Fargate\n\nCopilot uses Fargate.\n\n## Troubleshooting\n\n### Copilot init Freezes\n\nIf `copilot init` freezes immediately after the intro statement of \"Welcome to the Copilot CLI...,\" then this is a sign your AWS config and/or credentials file does not have a properly set up default AWS profile.\n\n### Health Check Fails\n\nFlask by default uses port 5000. Checking the logs shows `Running on http://10.0.0.207:5000/`, for example, using the following command.\n\n```bash\ncopilot svc logs -a [application-name] -n [service-name] --follow\n```\n\nCheck the Target Group port and make sure it is port 5000. This can be changed by updating the manifest file (image/port) and the dockerfile (`EXPOSE 5000`).\n\n#TODO: Explore changing the app/container port from 5000 to a custom value.\n\n## CloudWatch Container Insights\n\nMetrics include utilization for resources such as CPU, memory, disk, and network. Diagnostic info is also available, such as container restart failures.\n\n### (1) Install JQ\n\nMake sure you have jq installed to extract and transform JSON data. If on a Mac, use Homebrew.\n\nOtherwise use sudo yum: `sudo yum -y install jq gettext`\n\n### (2) Set Environment Variables\n\nGet the cluster arn by checking the clusters available in the AWS account and finding a match of a string:\n\n```bash\ncluster_arn=$(aws ecs list-clusters --profile 1s-sandbox | jq -r '.clusterArns[] | select(contains(\"sharina-ecs-copilot\"))')\n```\n\nGet the cluster name using variable created above:\n\n```bash\nclustername=$(aws ecs describe-clusters --profile 1s-sandbox --clusters $cluster_arn | jq -r '.clusters[].clusterName')\n```\n\nIdentify the region you're working in:\n\n```bash\nexport region=us-west-2\n```\n\n### (3) Enable Container Insights\n\nEnables Service and Cluster level insights on the targeted ECS cluster:\n\n```bash\naws ecs update-cluster-settings --cluster ${clustername}  --settings name=containerInsights,value=enabled --region $region --profile 1s-sandbox\n```\n\nThe output will be a block of info about the cluster, a portion of which will show that monitoring is enabled.\nWhen you go into the ECS console, next to the cluster name will be a green check that says \"Container Insights.\" This is in place of a black check next to \"Default Monitoring\" that was in its place.\n\n### Install Instance Level Insights on the ECS Cluster\n\nThe following command creates a stack from a CloudFormation template that deploys a Service into the cluster. More about this in the [CloudWatch docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/deploy-container-insights-ECS-instancelevel.html).\n\n```bash\naws cloudformation create-stack --stack-name CWAgentECS-$clustername-${region} --template-body \"$(curl -Ls https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/ecs-task-definition-templates/deployment-mode/daemon-service/cwagent-ecs-instance-metric/cloudformation-quickstart/cwagent-ecs-instance-metric-cfn.json)\" --parameters ParameterKey=ClusterName,ParameterValue=$clustername ParameterKey=CreateIAMRoles,ParameterValue=True --capabilities CAPABILITY_NAMED_IAM --region ${region} --profile 1s-sandbox\n```\n\nWhen you go to the console, there will now be an additional service in the cluster. That service does not have a task running.\n\n### Examine the Findings\n\nWhen you go to CloudWatch Log Groups in the console, the name of the log group is something like\n`/aws/ecs/containerinsights/sharina-ecs-copilot-sandbox-Cluster-...` and it will immediately have a bunch of log events to examine.\n\nThe CloudWatch console's home page has a dropdown menu next to the word \"CloudWatch.\" Choose `CloudWatch Insights`. Select ECS Clusters in the first dropdown and select the ECS cluster created in the second dropdown. There will be several built-in charts showing various cluster level metrics such as CPU Utilization, Memory Utilization, Network and other information. Clusters, Services and Tasks will all have data points.\n\nWhile clusters is chosen in from the dropdown, look near the bottom of the page to find the cluster name. Choose the radio button for the cluster and click on the Actions dropdown menu to select Performance Logs. This will take you to CloudWatch Logs and into a specialized search.\n\n## Load Testing\n\nHere are two easy-to-implement load testing options - Hey and Siege. I went with Hey, but if you'd prefer to try Siege, install with Homebrew grab the ALB URL and run with the following command:\n\n```bash\nsiege -c 200 -i $alb_url\n```\n\nThis command tells Siege to run 200 concurrent connections to the ECS application at varying URLS.\n\n### Install Hey\n\nIf on a Mac, use Homebrew to install: `brew install hey`. It will take a few minutes for everything to kick in, but if you have the manifest file to have scaling of the number of tasks based on CPU, you'll notice the number of tasks increase. More about hey [here](https://github.com/rakyll/hey).\n\n```bash\nhey -z 20m http://shari-Publi-7NOS5W3L1E5V-1195326126.us-west-2.elb.amazonaws.com\n```\nCheck the ECS console, CloudWatch alarms, CloudWatch Insights and use the terminal to get Copilot to produce lots of data using `copilot svc show` and `copilot svc status`.\n\nAfter about 20 minutes, hey will produce a report in the terminal with a summary of the test.\n\nTarget tracking is used, which is where when the CPU hits a certain target, it will compare data points collected up to this point to where it is now.\n\n## Get the CloudFormation that Copilot Created\n\nTo read the cloudformation for the service, `copilot svc package`.\n\nTo print or save the cloudformation use one of the options available with `copilot svc package help`. This allows you to take those resources and edit and use them as you like.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharinas%2Faws-copilot-containerized-flask-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsharinas%2Faws-copilot-containerized-flask-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharinas%2Faws-copilot-containerized-flask-app/lists"}