What it is
AWS Copilot CLI is a command-line interface for building, releasing, and operating containerized applications on AWS, simplifying the deployment of microservices and web applications.
Installation
Linux:
sudo curl -Lo /usr/local/bin/copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-linux-amd64
sudo chmod +x /usr/local/bin/copilot
Mac:
brew tap aws/tap
brew install copilot
Alternatively, download the binary:
curl -Lo copilot https://github.com/aws/copilot-cli/releases/latest/download/copilot-macos
chmod +x copilot
sudo mv copilot /usr/local/bin/copilot
Windows: Download the latest release from the Copilot CLI GitHub releases page and add the executable to your system’s PATH.
Core Concepts
- Application: A logical grouping of services. You deploy an application to an environment.
- Environment: A deployment target (e.g.,
test,staging,prod) that contains a set of AWS resources managed by Copilot. Environments can be linked, allowing for progressive rollouts. - Service: A specific containerized application (e.g., a web frontend, a backend API). A service can be deployed to multiple environments.
- Manifest File: A YAML file (e.g.,
copilot.yml,Dockerfile) that defines the configuration for a service or an environment.
Commands / Usage
Application Management
-
Initialize a new application:
copilot app init --name my-appCreates a new Copilot application named
my-app. -
List applications:
copilot app lsLists all Copilot applications in your AWS account and region.
-
Show application details:
copilot app show --name my-appDisplays details about the specified application.
-
Delete an application:
copilot app delete --name my-appDeletes the specified application and all its associated environments and services.
Environment Management
-
Initialize a new environment:
copilot env init --name test --profile defaultInitializes a new environment named
testusing your AWS default profile. You can specify a different profile with--profile <profile-name>. -
Deploy an environment:
copilot env deploy --name testDeploys or updates the specified environment.
-
List environments:
copilot env ls --name my-appLists all environments for the specified application.
-
Show environment details:
copilot env show --name my-app --env testDisplays details about the specified environment.
-
Delete an environment:
copilot env delete --name my-app --env testDeletes the specified environment and all services deployed within it.
Service Management
-
Initialize a new service:
copilot svc init --name api --app my-app --profile defaultInitializes a new service named
apiwithin themy-appapplication. You’ll be prompted to choose a service type (e.g., Load Balanced Web Service, Backend Service, Worker Service). This command creates acopilot.ymlfile for your service. -
Deploy a service:
copilot svc deploy --name api --app my-app --env testDeploys or updates the
apiservice to thetestenvironment for themy-appapplication. -
List services:
copilot svc ls --name my-app --env testLists all services deployed in the specified environment for the application.
-
Show service details:
copilot svc show --name my-app --svc api --env testDisplays details about the specified service in the environment.
-
View service logs:
copilot svc logs --name my-app --svc api --env testFetches and displays logs for the specified service in the environment. Use
--followto stream logs. -
Delete a service:
copilot svc delete --name my-app --svc api --env testDeletes the specified service from the environment.
Pipeline Management
-
Initialize a new pipeline:
copilot pipeline init --name my-pipeline --app my-appInitializes a new CI/CD pipeline for your application. You’ll be prompted to choose a source provider (e.g., GitHub, CodeCommit).
-
Deploy a pipeline:
copilot pipeline deploy --name my-pipeline --app my-appDeploys or updates the specified pipeline.
-
Show pipeline details:
copilot pipeline show --name my-pipeline --app my-appDisplays details about the specified pipeline, including its stages and associated environments.
-
Execute a pipeline:
copilot pipeline execute --name my-pipeline --app my-app --branch mainManually triggers a pipeline execution for a specific branch.
-
Delete a pipeline:
copilot pipeline delete --name my-pipeline --app my-appDeletes the specified pipeline.
Workspace Management
-
Initialize a new workspace:
copilot workspace initInitializes a new Copilot workspace if you’re not already in an application directory.
-
Show workspace status:
copilot workspace statusDisplays the current status of your Copilot workspace, including the application and environment context.
Other Commands
-
Check Copilot version:
copilot versionDisplays the installed version of the Copilot CLI.
-
Help:
copilot --help copilot app --help copilot svc deploy --helpProvides help information for commands and subcommands.
Common Patterns
-
Deploying a service to all environments:
copilot svc deploy --name api --app my-app --all-envsDeploys the
apiservice to every environment configured for themy-appapplication. -
Viewing logs for a service in the current environment:
copilot svc logs --svc api --followStreams logs for the
apiservice in the environment currently configured in your Copilot workspace. -
Creating a new service from a Dockerfile: When initializing a service (
copilot svc init), choose the "Load Balanced Web Service" or "Backend Service" type, and Copilot will prompt you to specify the path to yourDockerfile. -
Troubleshooting a failed deployment: Check the output of
copilot svc deployfor error messages. If the issue persists, examine CloudFormation stack events for the environment or service in the AWS console. Usecopilot svc logsto inspect application logs. -
Adding a new environment to an existing application:
- Initialize the environment:
copilot env init --name staging --app my-app - Deploy the environment:
copilot env deploy --name staging --app my-app - Deploy your services to the new environment:
copilot svc deploy --name <service-name> --app my-app --env staging
- Initialize the environment:
-
Configuring environment variables for a service: Edit the
copilot.ymlfile for your service. Under thevariablessection, add key-value pairs. For secrets, use AWS Secrets Manager.# copilot.yml name: my-api-service type: Backend Service variables: MY_VARIABLE: "some_value"
{% raw %} MY_SECRET: "{{secrets.my-app/my-secret-name}}" # Refers to a secret in AWS Secrets Manager {% endraw %} ```
-
Setting up health checks for a Load Balanced Web Service: In your
copilot.yml, configure thehealthcheckblock:# copilot.yml name: my-web-frontend type: Load Balanced Web Service healthcheck: path: /health interval: 30s timeout: 5s healthy_threshold: 2 unhealthy_threshold: 3
Gotchas
- Resource Naming: Copilot automatically generates AWS resource names based on your application, environment, and service names. Avoid using special characters or excessively long names, as this can sometimes lead to naming conflicts or errors.
- Permissions: Ensure your AWS credentials have the necessary permissions to create and manage AWS resources (ECS, ECR, ALB, CloudFormation, etc.). Copilot uses IAM roles, but the initial setup of your AWS environment might require specific permissions.
- Environment Linking: When linking environments (e.g.,
testtostaging), the linked environment’s resources are created within the same VPC as the source environment. This can simplify networking but also means they share the same network infrastructure. copilot.ymlLocation: Thecopilot.ymlfile must be in the root directory of your service’s codebase.- Implicit Resource Creation: Copilot abstracts away much of the underlying AWS infrastructure. While convenient, it means you might not always be aware of every resource being created. Understand the service types and their default configurations.
- State Management: Copilot manages your infrastructure through CloudFormation. Direct modifications to resources managed by Copilot outside of the CLI can lead to drift and deployment issues. Always use Copilot commands to manage your infrastructure.
- Service Discovery: For backend services communicating with each other, Copilot automatically configures Service Discovery using AWS Cloud Map. Ensure your service names are unique within the application.
- Docker Image Building: By default, Copilot builds Docker images using
docker buildin your local environment. If you need more advanced build steps or want to build directly on AWS, consider using custom build configurations or AWS CodeBuild within your pipeline.