Skip to content

Can't run task on environment which imported subnets #2022

@ota42y

Description

@ota42y

Overview

We can't run copilot task run with --env and --appp option.
The specified environment created by --import-public-subnets option.

The error message like this.

✘ run task migration-task: no subnets found

When copilot run task on environment, we require copilot-application and copilot-environment tags to subnet.
copilot init env command don't add these tags to imported subnets, so copilot can't find environment's subnets.
However, it is very confusing because we can run services without these tags, and it is not documented and cannot be determined from error message.

Therefore, we should modify the error messages to make them easier to understand, or to get subnets without using tags in the same way that service.

Detail

Create an environment named stg by following command using the VPC and subnet created by other way(web console, aws cli, terraform...).

$ copilot env init -a myapp -n stg \
   --import-private-subnets subnet-5678 --import-public-subnets subnet-1234 --import-vpc-id vpc-1234

We get no subnet found error when we run task to stg by following command.

$ copilot task run \
   --task-role stg-instance-role \
   -n migration-task \
   --dockerfile Dockerfile \
   --command /run.sh \
   --env stg \
   --app myapp
......

✘ Failed to run migration-task.

✘ run task migration-task: no subnets found

The copilot gets subnets which added copilot-application and copilot-environment tags.
But when we created other way, we don't add these tags and copilot env init command doesn't add these tags so copilot can't find subnets.
https://github.com/aws/copilot-cli/blob/2d6d29b5d826cdfc37e48b3e2dbb0cfb73517bc8/internal/pkg/task/env_runner.go#L58-60

This requirements is not documented and we can't undestand from error message.
Also, this only happens when executing task, and service works fine, so it is more difficult to determine.

Solution

Fix error message

We show more readable error message like this.
#2023

This won't solve the problem, but it can guide user to a solution in many cases.

Get subnets fron environment's stack

copilot use CloudFormation to deploy services and it uses the output of the environment's stack to get subnets.
So when we deploy services, it works without tags in subnets.

AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- Fn::Select:
- 0
- Fn::Split:
- ','
- Fn::ImportValue: !Sub '${AppName}-${EnvName}-PublicSubnets'

If the user specify --app and --env, it is assumed to run on the same setting as service.
Therefore, it is more good to get public subnets from stack, and we can solve this bugs.

Also, depending on the tags, it may be possible to get a different subnet which doesn't associated with the envrionment.
This may lead to more complicated bugs, so it is safer to obtain the tag from the stack.
If we want to use a different subnet, we can use the --subnets option, so when the user specify --env option we should not be able to change it.

Also, when specifying tags, only one application can be tied to a subnet, which may be useful when multiple teams and application use copilot.

I add this changes to get subnets from environment's stack
#2024

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions