Skip to content

Commit

Permalink
fix: allow --container-env-var-file in sync command (aws#6427)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitintecg committed May 4, 2024
1 parent 624d3d5 commit f22232b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions samcli/commands/sync/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@
help="This option will skip the initial infrastructure deployment if it is not required"
" by comparing the local template with the template deployed in cloud.",
)
@click.option(
"--container-env-var-file",
"-ef",
default=None,
type=click.Path(), # Must be a json file
help="Environment variables json file (e.g., env_vars.json) to be passed to containers.",
cls=ContainerOptions,
)
@watch_exclude_option
@stack_name_option(required=True) # pylint: disable=E1120
@base_dir_option
Expand Down Expand Up @@ -210,6 +218,7 @@ def cli(
tags: dict,
metadata: dict,
use_container: bool,
container_env_var_file: Optional[str],
save_params: bool,
config_file: str,
config_env: str,
Expand Down Expand Up @@ -248,6 +257,7 @@ def cli(
tags,
metadata,
use_container,
container_env_var_file,
build_image,
config_file,
config_env,
Expand Down Expand Up @@ -281,6 +291,7 @@ def do_cli(
tags: dict,
metadata: dict,
use_container: bool,
container_env_var_file: Optional[str],
build_image: Optional[Tuple[str]],
config_file: str,
config_env: str,
Expand Down Expand Up @@ -331,6 +342,7 @@ def do_cli(
cache_dir=DEFAULT_CACHE_DIR,
clean=True,
use_container=use_container,
container_env_var_file=container_env_var_file,
cached=True,
parallel=True,
parameter_overrides=parameter_overrides,
Expand Down
7 changes: 6 additions & 1 deletion schema/samcli.json
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@
"properties": {
"parameters": {
"title": "Parameters for the sync command",
"description": "Available parameters for the sync command:\n* template_file:\nAWS SAM template file.\n* code:\nSync ONLY code resources. This includes Lambda Functions, API Gateway, and Step Functions.\n* watch:\nWatch local files and automatically sync with cloud.\n* resource_id:\nSync code for all the resources with the ID. To sync a resource within a nested stack, use the following pattern {ChildStack}/{logicalId}.\n* resource:\nSync code for all resources of the given resource type. Accepted values are ['AWS::Serverless::Function', 'AWS::Lambda::Function', 'AWS::Serverless::LayerVersion', 'AWS::Lambda::LayerVersion', 'AWS::Serverless::Api', 'AWS::ApiGateway::RestApi', 'AWS::Serverless::HttpApi', 'AWS::ApiGatewayV2::Api', 'AWS::Serverless::StateMachine', 'AWS::StepFunctions::StateMachine']\n* dependency_layer:\nSeparate dependencies of individual function into a Lambda layer for improved performance.\n* skip_deploy_sync:\nThis option will skip the initial infrastructure deployment if it is not required by comparing the local template with the template deployed in cloud.\n* watch_exclude:\nExcludes a file or folder from being observed for file changes. Files and folders that are excluded will not trigger a sync workflow. This option can be provided multiple times.\n\nExamples:\n\nHelloWorldFunction=package-lock.json\n\nChildStackA/FunctionName=database.sqlite3\n* stack_name:\nName of the AWS CloudFormation stack.\n* base_dir:\nResolve relative paths to function's source code with respect to this directory. Use this if SAM template and source code are not in same enclosing folder. By default, relative paths are resolved with respect to the SAM template's location.\n* use_container:\nBuild functions within an AWS Lambda-like container.\n* build_in_source:\nOpts in to build project in the source folder. The following workflows support building in source: ['nodejs16.x', 'nodejs18.x', 'nodejs20.x', 'Makefile', 'esbuild']\n* build_image:\nContainer image URIs for building functions/layers. You can specify for all functions/layers with just the image URI (--build-image public.ecr.aws/sam/build-nodejs18.x:latest). You can specify for each individual function with (--build-image FunctionLogicalID=public.ecr.aws/sam/build-nodejs18.x:latest). A combination of the two can be used. If a function does not have build image specified or an image URI for all functions, the default SAM CLI build images will be used.\n* image_repository:\nAWS ECR repository URI where artifacts referenced in the template are uploaded.\n* image_repositories:\nMapping of Function Logical ID to AWS ECR Repository URI.\n\nExample: Function_Logical_ID=ECR_Repo_Uri\nThis option can be specified multiple times.\n* s3_bucket:\nAWS S3 bucket where artifacts referenced in the template are uploaded.\n* s3_prefix:\nPrefix name that is added to the artifact's name when it is uploaded to the AWS S3 bucket.\n* kms_key_id:\nThe ID of an AWS KMS key that is used to encrypt artifacts that are at rest in the AWS S3 bucket.\n* role_arn:\nARN of an IAM role that AWS Cloudformation assumes when executing a deployment change set.\n* parameter_overrides:\nString that contains AWS CloudFormation parameter overrides encoded as key=value pairs.\n* beta_features:\nEnable/Disable beta features.\n* debug:\nTurn on debug logging to print debug message generated by AWS SAM CLI and display timestamps.\n* profile:\nSelect a specific profile from your credential file to get AWS credentials.\n* region:\nSet the AWS Region of the service. (e.g. us-east-1)\n* metadata:\nMap of metadata to attach to ALL the artifacts that are referenced in the template.\n* notification_arns:\nARNs of SNS topics that AWS Cloudformation associates with the stack.\n* tags:\nList of tags to associate with the stack.\n* capabilities:\nList of capabilities that one must specify before AWS Cloudformation can create certain stacks.\n\nAccepted Values: CAPABILITY_IAM, CAPABILITY_NAMED_IAM, CAPABILITY_RESOURCE_POLICY, CAPABILITY_AUTO_EXPAND.\n\nLearn more at: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/acknowledging-application-capabilities.html\n* save_params:\nSave the parameters provided via the command line to the configuration file.",
"description": "Available parameters for the sync command:\n* template_file:\nAWS SAM template file.\n* code:\nSync ONLY code resources. This includes Lambda Functions, API Gateway, and Step Functions.\n* watch:\nWatch local files and automatically sync with cloud.\n* resource_id:\nSync code for all the resources with the ID. To sync a resource within a nested stack, use the following pattern {ChildStack}/{logicalId}.\n* resource:\nSync code for all resources of the given resource type. Accepted values are ['AWS::Serverless::Function', 'AWS::Lambda::Function', 'AWS::Serverless::LayerVersion', 'AWS::Lambda::LayerVersion', 'AWS::Serverless::Api', 'AWS::ApiGateway::RestApi', 'AWS::Serverless::HttpApi', 'AWS::ApiGatewayV2::Api', 'AWS::Serverless::StateMachine', 'AWS::StepFunctions::StateMachine']\n* dependency_layer:\nSeparate dependencies of individual function into a Lambda layer for improved performance.\n* skip_deploy_sync:\nThis option will skip the initial infrastructure deployment if it is not required by comparing the local template with the template deployed in cloud.\n* container_env_var_file:\nEnvironment variables json file (e.g., env_vars.json) to be passed to containers.\n* watch_exclude:\nExcludes a file or folder from being observed for file changes. Files and folders that are excluded will not trigger a sync workflow. This option can be provided multiple times.\n\nExamples:\n\nHelloWorldFunction=package-lock.json\n\nChildStackA/FunctionName=database.sqlite3\n* stack_name:\nName of the AWS CloudFormation stack.\n* base_dir:\nResolve relative paths to function's source code with respect to this directory. Use this if SAM template and source code are not in same enclosing folder. By default, relative paths are resolved with respect to the SAM template's location.\n* use_container:\nBuild functions within an AWS Lambda-like container.\n* build_in_source:\nOpts in to build project in the source folder. The following workflows support building in source: ['nodejs16.x', 'nodejs18.x', 'nodejs20.x', 'Makefile', 'esbuild']\n* build_image:\nContainer image URIs for building functions/layers. You can specify for all functions/layers with just the image URI (--build-image public.ecr.aws/sam/build-nodejs18.x:latest). You can specify for each individual function with (--build-image FunctionLogicalID=public.ecr.aws/sam/build-nodejs18.x:latest). A combination of the two can be used. If a function does not have build image specified or an image URI for all functions, the default SAM CLI build images will be used.\n* image_repository:\nAWS ECR repository URI where artifacts referenced in the template are uploaded.\n* image_repositories:\nMapping of Function Logical ID to AWS ECR Repository URI.\n\nExample: Function_Logical_ID=ECR_Repo_Uri\nThis option can be specified multiple times.\n* s3_bucket:\nAWS S3 bucket where artifacts referenced in the template are uploaded.\n* s3_prefix:\nPrefix name that is added to the artifact's name when it is uploaded to the AWS S3 bucket.\n* kms_key_id:\nThe ID of an AWS KMS key that is used to encrypt artifacts that are at rest in the AWS S3 bucket.\n* role_arn:\nARN of an IAM role that AWS Cloudformation assumes when executing a deployment change set.\n* parameter_overrides:\nString that contains AWS CloudFormation parameter overrides encoded as key=value pairs.\n* beta_features:\nEnable/Disable beta features.\n* debug:\nTurn on debug logging to print debug message generated by AWS SAM CLI and display timestamps.\n* profile:\nSelect a specific profile from your credential file to get AWS credentials.\n* region:\nSet the AWS Region of the service. (e.g. us-east-1)\n* metadata:\nMap of metadata to attach to ALL the artifacts that are referenced in the template.\n* notification_arns:\nARNs of SNS topics that AWS Cloudformation associates with the stack.\n* tags:\nList of tags to associate with the stack.\n* capabilities:\nList of capabilities that one must specify before AWS Cloudformation can create certain stacks.\n\nAccepted Values: CAPABILITY_IAM, CAPABILITY_NAMED_IAM, CAPABILITY_RESOURCE_POLICY, CAPABILITY_AUTO_EXPAND.\n\nLearn more at: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/acknowledging-application-capabilities.html\n* save_params:\nSave the parameters provided via the command line to the configuration file.",
"type": "object",
"properties": {
"template_file": {
Expand Down Expand Up @@ -1611,6 +1611,11 @@
"description": "This option will skip the initial infrastructure deployment if it is not required by comparing the local template with the template deployed in cloud.",
"default": true
},
"container_env_var_file": {
"title": "container_env_var_file",
"type": "string",
"description": "Environment variables json file (e.g., env_vars.json) to be passed to containers."
},
"watch_exclude": {
"title": "watch_exclude",
"type": "array",
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ def test_sync(
"notification_arns": "notify1 notify2",
"tags": 'a=tag1 b="tag with spaces"',
"metadata": '{"m1": "value1", "m2": "value2"}',
"container_env_var_file": "file",
"guided": True,
"confirm_changeset": True,
"region": "myregion",
Expand Down Expand Up @@ -1016,6 +1017,7 @@ def test_sync(
{"a": "tag1", "b": "tag with spaces"},
{"m1": "value1", "m2": "value2"},
True,
"file",
(),
"samconfig.toml",
"default",
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/commands/sync/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def setUp(self):
self.config_env = "mock-default-env"
self.config_file = "mock-default-filename"
self.build_image = None
self.container_env_var_file = "file"
MOCK_SAM_CONFIG.reset_mock()

@parameterized.expand(
Expand Down Expand Up @@ -142,6 +143,7 @@ def test_infra_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.container_env_var_file,
self.build_image,
self.config_file,
self.config_env,
Expand All @@ -161,6 +163,7 @@ def test_infra_must_succeed_sync(
cache_dir=DEFAULT_CACHE_DIR,
clean=True,
use_container=use_container,
container_env_var_file=self.container_env_var_file,
parallel=True,
parameter_overrides=self.parameter_overrides,
mode=self.mode,
Expand Down Expand Up @@ -302,6 +305,7 @@ def test_watch_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.container_env_var_file,
self.build_image,
self.config_file,
self.config_env,
Expand All @@ -317,6 +321,7 @@ def test_watch_must_succeed_sync(
cache_dir=DEFAULT_CACHE_DIR,
clean=True,
use_container=use_container,
container_env_var_file=self.container_env_var_file,
parallel=True,
parameter_overrides=self.parameter_overrides,
mode=self.mode,
Expand Down Expand Up @@ -451,6 +456,7 @@ def test_code_must_succeed_sync(
self.tags,
self.metadata,
use_container,
self.container_env_var_file,
self.build_image,
self.config_file,
self.config_env,
Expand Down

0 comments on commit f22232b

Please sign in to comment.