Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sam pipelines ux rename ecr repo to image repository #2910

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions samcli/commands/pipeline/bootstrap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
\n\t - Pipeline execution IAM role assumed by the pipeline user to obtain access to the AWS account
\n\t - CloudFormation execution IAM role assumed by CloudFormation to deploy the AWS SAM application
\n\t - Artifacts S3 bucket to hold the AWS SAM build artifacts
\n\t - Optionally, an ECR repository to hold container image Lambda deployment packages
\n\t - Optionally, an ECR image repository to hold container image Lambda deployment packages
"""

PIPELINE_CONFIG_DIR = os.path.join(".aws-sam", "pipeline")
Expand Down Expand Up @@ -68,17 +68,17 @@
required=False,
)
@click.option(
"--create-ecr-repo/--no-create-ecr-repo",
"--create-image-repository/--no-create-image-repository",
is_flag=True,
default=False,
help="If set to true and no ECR repository is provided, this command will create an ECR repository to hold the"
" container images of Lambda functions having an Image package type.",
help="If set to true and no ECR image repository is provided, this command will create an ECR image repository "
"to hold the container images of Lambda functions having an Image package type.",
)
@click.option(
"--ecr-repo",
help="The ARN of an ECR repository to hold the containers images of Lambda functions of Image package type. "
"If provided, the --create-ecr-repo argument is ignored. If not provided and --create-ecr-repo is set to true, "
"the command will create one.",
"--image-repository",
help="The ARN of an ECR image repository to hold the containers images of Lambda functions of Image package type. "
"If provided, the --create-image-repository argument is ignored. If not provided and --create-image-repository is "
"set to true, the command will create one.",
required=False,
)
@click.option(
Expand Down Expand Up @@ -106,8 +106,8 @@ def cli(
pipeline_execution_role: Optional[str],
cloudformation_execution_role: Optional[str],
artifacts_bucket: Optional[str],
create_ecr_repo: bool,
ecr_repo: Optional[str],
create_image_repository: bool,
image_repository: Optional[str],
pipeline_ip_range: Optional[str],
confirm_changeset: bool,
config_file: Optional[str],
Expand All @@ -125,8 +125,8 @@ def cli(
pipeline_execution_role_arn=pipeline_execution_role,
cloudformation_execution_role_arn=cloudformation_execution_role,
artifacts_bucket_arn=artifacts_bucket,
create_ecr_repo=create_ecr_repo,
ecr_repo_arn=ecr_repo,
create_image_repository=create_image_repository,
image_repository_arn=image_repository,
pipeline_ip_range=pipeline_ip_range,
confirm_changeset=confirm_changeset,
config_file=config_env,
Expand All @@ -143,8 +143,8 @@ def do_cli(
pipeline_execution_role_arn: Optional[str],
cloudformation_execution_role_arn: Optional[str],
artifacts_bucket_arn: Optional[str],
create_ecr_repo: bool,
ecr_repo_arn: Optional[str],
create_image_repository: bool,
image_repository_arn: Optional[str],
pipeline_ip_range: Optional[str],
confirm_changeset: bool,
config_file: Optional[str],
Expand All @@ -163,8 +163,8 @@ def do_cli(
pipeline_execution_role_arn=pipeline_execution_role_arn,
cloudformation_execution_role_arn=cloudformation_execution_role_arn,
artifacts_bucket_arn=artifacts_bucket_arn,
create_ecr_repo=create_ecr_repo,
ecr_repo_arn=ecr_repo_arn,
create_image_repository=create_image_repository,
image_repository_arn=image_repository_arn,
pipeline_ip_range=pipeline_ip_range,
)
guided_context.run()
Expand All @@ -174,8 +174,8 @@ def do_cli(
pipeline_ip_range = guided_context.pipeline_ip_range
cloudformation_execution_role_arn = guided_context.cloudformation_execution_role_arn
artifacts_bucket_arn = guided_context.artifacts_bucket_arn
create_ecr_repo = guided_context.create_ecr_repo
ecr_repo_arn = guided_context.ecr_repo_arn
create_image_repository = guided_context.create_image_repository
image_repository_arn = guided_context.image_repository_arn

if not environment_name:
raise click.UsageError("Missing required parameter '--environment'")
Expand All @@ -189,8 +189,8 @@ def do_cli(
pipeline_ip_range=pipeline_ip_range,
cloudformation_execution_role_arn=cloudformation_execution_role_arn,
artifacts_bucket_arn=artifacts_bucket_arn,
create_ecr_repo=create_ecr_repo,
ecr_repo_arn=ecr_repo_arn,
create_image_repository=create_image_repository,
image_repository_arn=image_repository_arn,
)

bootstrapped: bool = environment.bootstrap(confirm_changeset=confirm_changeset)
Expand Down
22 changes: 11 additions & 11 deletions samcli/commands/pipeline/bootstrap/guided_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def __init__(
pipeline_execution_role_arn: Optional[str] = None,
cloudformation_execution_role_arn: Optional[str] = None,
artifacts_bucket_arn: Optional[str] = None,
create_ecr_repo: bool = False,
ecr_repo_arn: Optional[str] = None,
create_image_repository: bool = False,
image_repository_arn: Optional[str] = None,
pipeline_ip_range: Optional[str] = None,
) -> None:
self.environment_name = environment_name
self.pipeline_user_arn = pipeline_user_arn
self.pipeline_execution_role_arn = pipeline_execution_role_arn
self.cloudformation_execution_role_arn = cloudformation_execution_role_arn
self.artifacts_bucket_arn = artifacts_bucket_arn
self.create_ecr_repo = create_ecr_repo
self.ecr_repo_arn = ecr_repo_arn
self.create_image_repository = create_image_repository
self.image_repository_arn = image_repository_arn
self.pipeline_ip_range = pipeline_ip_range

def run(self) -> None:
Expand Down Expand Up @@ -70,22 +70,22 @@ def run(self) -> None:
default="",
type=click.STRING,
)
if not self.ecr_repo_arn:
if not self.image_repository_arn:
click.echo(
"\nIf your SAM template includes (or going to include) Lambda functions of Image package type, "
"then an ECR repository is required. Should we create one?"
"then an ECR image repository is required. Should we create one?"
)
click.echo("\t1 - No, My SAM template won't include Lambda functions of Image package type")
click.echo("\t2 - Yes, I need help creating one")
click.echo("\t3 - I already have an ECR repository")
click.echo("\t3 - I already have an ECR image repository")
choice = click.prompt(text="Choice", show_choices=False, type=click.Choice(["1", "2", "3"]))
if choice == "1":
self.create_ecr_repo = False
self.create_image_repository = False
elif choice == "2":
self.create_ecr_repo = True
self.create_image_repository = True
else: # choice == "3"
self.create_ecr_repo = False
self.ecr_repo_arn = click.prompt("ECR repo", type=click.STRING)
self.create_image_repository = False
self.image_repository_arn = click.prompt("ECR image repository", type=click.STRING)

if not self.pipeline_ip_range:
click.echo("\nWe can deny requests not coming from a recognized IP address range.")
Expand Down
1 change: 0 additions & 1 deletion samcli/commands/pipeline/init/interactive_init_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def _load_pipeline_bootstrap_context() -> Dict:
config = SamConfig(PIPELINE_CONFIG_DIR, PIPELINE_CONFIG_FILENAME)
if not config.exists():
return context

for env in config.get_env_names():
for key, value in config.get_all(bootstrap_command_names, section, env).items():
context[str([env, key])] = value
Expand Down
52 changes: 26 additions & 26 deletions samcli/lib/pipeline/bootstrap/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from samcli.lib.config.samconfig import SamConfig
from samcli.lib.utils.managed_cloudformation_stack import manage_stack, StackOutput
from .resource import Resource, IAMUser, ECRRepo
from .resource import Resource, IAMUser, ECRImageRepository

CFN_TEMPLATE_PATH = str(pathlib.Path(os.path.dirname(__file__)))
STACK_NAME_PREFIX = "aws-sam-cli-managed"
Expand All @@ -18,7 +18,7 @@
PIPELINE_EXECUTION_ROLE = "pipeline_execution_role"
CLOUDFORMATION_EXECUTION_ROLE = "cloudformation_execution_role"
ARTIFACTS_BUCKET = "artifacts_bucket"
ECR_REPO = "ecr_repo"
ECR_IMAGE_REPOSITORY = "image_repository"
REGION = "region"


Expand Down Expand Up @@ -47,24 +47,24 @@ class Environment:
The IAM role assumed by the CloudFormation service to executes the CloudFormation stack.
artifacts_bucket: Resource
The S3 bucket to hold the SAM build artifacts of the application's CFN template.
create_ecr_repo: bool
A boolean flag that determins whether the user wants to create an ECR repository or not
ecr_repo: ECRRepo
The ECR repo to hold the image container of lambda functions with Image package-type
create_image_repository: bool
A boolean flag that determines whether the user wants to create an ECR image repository or not
image_repository: ECRImageRepository
The ECR image repository to hold the image container of lambda functions with Image package-type

Methods:
--------
did_user_provide_all_required_resources(self) -> bool:
checks if all of the environment's required resources (pipeline_user, pipeline_execution_role,
cloudformation_execution_role, artifacts_bucket and ecr_repo) are provided by the user.
cloudformation_execution_role, artifacts_bucket and image_repository) are provided by the user.
bootstrap(self, confirm_changeset: bool = True) -> None:
deploys the CFN template ./environment_resources.yaml to the AWS account identified by aws_profile and
aws_region member fields. if aws_profile is not provided, it will fallback to default boto3 credentials'
resolving. Note that ./environment_resources.yaml template accepts the ARNs of already existing resources(if
any) as parameters and it will skip the creation of those resources but will use the ARNs to set the proper
permissions of other missing resources(resources created by the template)
save_config(self, config_dir: str, filename: str, cmd_names: List[str]):
save the Artifacts bucket name, ECR repo URI and ARNs of pipeline_user, pipeline_execution_role and
save the Artifacts bucket name, ECR image repository URI and ARNs of pipeline_user, pipeline_execution_role and
cloudformation_execution_role to the "pipelineconfig.toml" file so that it can be auto-filled during
the `sam pipeline init` command.
print_resources_summary(self) -> None:
Expand All @@ -81,8 +81,8 @@ def __init__(
pipeline_ip_range: Optional[str] = None,
cloudformation_execution_role_arn: Optional[str] = None,
artifacts_bucket_arn: Optional[str] = None,
create_ecr_repo: bool = False,
ecr_repo_arn: Optional[str] = None,
create_image_repository: bool = False,
image_repository_arn: Optional[str] = None,
) -> None:
self.name: str = name
self.aws_profile: Optional[str] = aws_profile
Expand All @@ -92,8 +92,8 @@ def __init__(
self.pipeline_ip_range: Optional[str] = pipeline_ip_range
self.cloudformation_execution_role: Resource = Resource(arn=cloudformation_execution_role_arn)
self.artifacts_bucket: Resource = Resource(arn=artifacts_bucket_arn)
self.create_ecr_repo: bool = create_ecr_repo
self.ecr_repo: ECRRepo = ECRRepo(arn=ecr_repo_arn)
self.create_image_repository: bool = create_image_repository
self.image_repository: ECRImageRepository = ECRImageRepository(arn=image_repository_arn)

def did_user_provide_all_required_resources(self) -> bool:
"""Check if the user provided all of the environment resources or not"""
Expand All @@ -109,8 +109,8 @@ def _get_non_user_provided_resources_msg(self) -> str:
missing_resources_msg += "\n\tCloudFormation execution role."
if not self.artifacts_bucket.is_user_provided:
missing_resources_msg += "\n\tArtifacts bucket."
if self.create_ecr_repo and not self.ecr_repo.is_user_provided:
missing_resources_msg += "\n\tECR repo."
if self.create_image_repository and not self.image_repository.is_user_provided:
missing_resources_msg += "\n\tECR image repository."
return missing_resources_msg

def bootstrap(self, confirm_changeset: bool = True) -> bool:
Expand All @@ -120,7 +120,7 @@ def bootstrap(self, confirm_changeset: bool = True) -> bool:
* Pipeline execution IAM role
* CloudFormation execution IAM role
* Artifacts' S3 Bucket
* ECR Repo
* ECR image repository
to the AWS account associated with the given environment. It will not redeploy the stack if already exists.
This CFN template accepts the ARNs of the resources as parameters and will not create a resource if already
provided, this way we can conditionally create a resource only if the user didn't provide it
Expand Down Expand Up @@ -166,8 +166,8 @@ def bootstrap(self, confirm_changeset: bool = True) -> bool:
"PipelineIpRange": self.pipeline_ip_range or "",
"CloudFormationExecutionRoleArn": self.cloudformation_execution_role.arn or "",
"ArtifactsBucketArn": self.artifacts_bucket.arn or "",
"CreateECRRepo": "true" if self.create_ecr_repo else "false",
"ECRRepoArn": self.ecr_repo.arn or "",
"CreateImageRepository": "true" if self.create_image_repository else "false",
"ImageRepositoryArn": self.image_repository.arn or "",
},
)

Expand All @@ -177,7 +177,7 @@ def bootstrap(self, confirm_changeset: bool = True) -> bool:
self.pipeline_execution_role.arn = output.get("PipelineExecutionRole")
self.cloudformation_execution_role.arn = output.get("CloudFormationExecutionRole")
self.artifacts_bucket.arn = output.get("ArtifactsBucket")
self.ecr_repo.arn = output.get("ECRRepo")
self.image_repository.arn = output.get("ImageRepository")
return True

@staticmethod
Expand All @@ -189,7 +189,7 @@ def _read_template(template_file_name: str) -> str:

def save_config(self, config_dir: str, filename: str, cmd_names: List[str]) -> None:
"""
save the Artifacts bucket name, ECR repo URI and ARNs of pipeline_user, pipeline_execution_role and
save the Artifacts bucket name, ECR image repository URI and ARNs of pipeline_user, pipeline_execution_role and
cloudformation_execution_role to the given filename and directory.

Parameters
Expand All @@ -203,30 +203,30 @@ def save_config(self, config_dir: str, filename: str, cmd_names: List[str]) -> N

Raises
------
ValueError: if the artifacts_bucket or ecr_repo ARNs are invalid
ValueError: if the artifacts_bucket or ImageRepository ARNs are invalid
"""

samconfig: SamConfig = SamConfig(config_dir=config_dir, filename=filename)

if self.pipeline_user.arn:
samconfig.put(cmd_names=cmd_names, section="parameters", key=PIPELINE_USER, value=self.pipeline_user.arn)

# Computing Artifacts bucket name and ECR repo URL may through an exception if the ARNs are wrong
# Computing Artifacts bucket name and ECR image repository URL may through an exception if the ARNs are wrong
# Let's swallow such an exception to be able to save the remaining resources
try:
artifacts_bucket_name: Optional[str] = self.artifacts_bucket.name()
except ValueError:
artifacts_bucket_name = ""
try:
ecr_repo_uri: Optional[str] = self.ecr_repo.get_uri()
image_repository_uri: Optional[str] = self.image_repository.get_uri()
except ValueError:
ecr_repo_uri = ""
image_repository_uri = ""

environment_specific_configs: Dict[str, Optional[str]] = {
PIPELINE_EXECUTION_ROLE: self.pipeline_execution_role.arn,
CLOUDFORMATION_EXECUTION_ROLE: self.cloudformation_execution_role.arn,
ARTIFACTS_BUCKET: artifacts_bucket_name,
ECR_REPO: ecr_repo_uri,
ECR_IMAGE_REPOSITORY: image_repository_uri,
REGION: self.aws_region,
}

Expand Down Expand Up @@ -258,8 +258,8 @@ def _get_resources(self) -> List[Resource]:
self.cloudformation_execution_role,
self.artifacts_bucket,
]
if self.create_ecr_repo: # ECR Repo is optional
resources.append(self.ecr_repo)
if self.create_image_repository or self.image_repository.arn: # Image Repository is optional
resources.append(self.image_repository)
return resources

def print_resources_summary(self) -> None:
Expand Down
Loading