aws / aws-cli

Universal Command Line Interface for Amazon Web Services
Other
15.29k stars 4.07k forks source link

In aws cloudformation package command, !Sub or !Ref function cannot work properly in Code property of AWS::Lambda::Function resource. #8782

Closed y-isono closed 1 month ago

y-isono commented 1 month ago

Describe the bug

aws cloudformation package command do not upload and convert local file when !Sub or !Ref function is used in Code property of AWS::Lambda::Function.

Expected Behavior

  1. In aws cloudformation package command, !Sub or !Ref function can be used and worked properly in Code property of AWS::Lambda::Function.

Current Behavior

aws cloudformation package command has error Property Code of xxx resource is not a URL and does not convert and upload local fire when !Sub or !Ref function is used in Code property of AWS::Lambda::Function

invoke.yaml

AWSTemplateFormatVersion: "2010-09-09"
Description: "Invoke main process via Lambda"

Parameters:
  func:
    Type: "String"
    Default: "lambda_function/lambda_handler"

Resources:
  InvocationLambda:
    Type: "AWS::Lambda::Function"
    Properties:
      FunctionName: SampleLambda
      Handler: "lambda_handler"
      Code: !Sub '${func}.py'
      Runtime: "python3.12"
tree
.
├── invoke.yaml
└── lambda_function
    └── lambda_handler.py

1 directory, 2 files

$ aws cloudformation package --template-file invoke.yaml --s3-bucket --s3-prefix lambda-package --output-template-file packaged-invoke.yaml --debug

(snipped)
2024-07-05 22:42:00,454 - MainThread - awscli.customizations.cloudformation.artifact_exporter - DEBUG - Property Code of InvocationLambda resource is not a URL

Successfully packaged artifacts and wrote output template to file packaged-invoke.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /home/ssm-user/tmp/packaged-invoke.yaml --stack-name <YOUR STACK NAME>

Reproduction Steps

same above

Possible Solution

It seems that export function does not work properly because isinstance turns false when !Sub or !Ref function is used so this code needs to be modify.

https://github.com/aws/aws-cli/blob/61e65888fe1c4fdebab35a85a4de0f1251f00164/awscli/customizations/cloudformation/artifact_exporter.py#L242-L245

Additional Information/Context

No response

CLI version used

aws-cli/2.17.9

Environment details (OS name and version, etc.)

Python/3.11.8 Linux/5.10.219-208.866.amzn2.x86_64 exe/x86_64.amzn.2

tim-finnigan commented 1 month ago

Thanks for reaching out. I'll link the CloudFormation package and deploy command documentation for reference. Have you tried using --parameter-overrides in your deploy command to pass the values?

For example: `invoke.yaml`: ```yaml AWSTemplateFormatVersion: '2010-09-09' Description: Invoke main process via Lambda Parameters: FunctionS3Bucket: Type: String Description: S3 bucket where the Lambda function code is stored FunctionS3Key: Type: String Description: S3 key (file path) of the Lambda function code LambdaRoleArn: Type: String Description: ARN of the IAM role for the Lambda function Resources: InvocationLambda: Type: AWS::Lambda::Function Properties: FunctionName: SampleLambda Handler: lambda_handler Code: S3Bucket: Ref: FunctionS3Bucket S3Key: Fn::Sub: ${FunctionS3Key} Runtime: python3.12 Role: Ref: LambdaRoleArn ``` `package` command: ``` aws cloudformation package \ --template-file invoke.yaml \ --s3-bucket my-bucket \ --s3-prefix lambda-package \ --output-template-file packaged-invoke.yaml ``` `packaged-invoke.yaml`: ```yaml AWSTemplateFormatVersion: '2010-09-09' Description: Invoke main process via Lambda Parameters: FunctionS3Bucket: Type: String Description: S3 bucket where the Lambda function code is stored FunctionS3Key: Type: String Description: S3 key (file path) of the Lambda function code LambdaRoleArn: Type: String Description: ARN of the IAM role for the Lambda function Resources: InvocationLambda: Type: AWS::Lambda::Function Properties: FunctionName: SampleLambda Handler: lambda_handler Code: S3Bucket: Ref: FunctionS3Bucket S3Key: Fn::Sub: ${FunctionS3Key} Runtime: python3.12 Role: Ref: LambdaRoleArn ``` `deploy` command: ``` aws cloudformation deploy \ --template-file packaged-invoke.yaml \ --stack-name my-stack \ --parameter-overrides \ FunctionS3Bucket=my-bucket\ FunctionS3Key=lambda-package/lambda_function.zip \ LambdaRoleArn=arn:aws:iam:::role/abc ```
amberkushwaha commented 1 month ago

cloudformation package deploy response requested customization p2 and removed labels 13 hours ago.customization p2 and removed investigating in the main stream in it. for the given time period and issues.

github-actions[bot] commented 1 month ago

Greetings! It looks like this issue hasn’t been active in longer than five days. We encourage you to check if this is still an issue in the latest release. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or upvote with a reaction on the initial post to prevent automatic closure. If the issue is already closed, please feel free to open a new one.