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

AWS::Serverless::Function CodeUri with Ref not working properly #271

Closed
jikuja opened this issue Jan 27, 2018 · 8 comments
Closed

AWS::Serverless::Function CodeUri with Ref not working properly #271

jikuja opened this issue Jan 27, 2018 · 8 comments
Labels
area/intrinsics Ref, If, Sub, GetAtt, ...

Comments

@jikuja
Copy link

jikuja commented Jan 27, 2018

I have following set of parameters:

   "Parameters" : {
      "DeployBucket" : {
         "Type" : "String",
         "Default": "a"
      },
      "DeployKey" : {
         "Type" : "String",
         "Default" : "b"
      },
      "DeployString": {
         "Type" : "String",
         "Default" : "s3://<some-bucket>/<somefile>"
      }
   }

in my template for testing.

hardcoded S3 URI inside CodeUri works

If I use "CodeUri" : "s3://<my-bucket>/<my-file>", with AWS::Serverless::Function resource I run aws deploy properly.

usage of key and bucket properties also works

Even using following

"CodeUri" : {
               "Key": {"Ref": "DeployKey"},
               "Bucket": {"Ref": "DeployBucket"}
            }

and running aws deploy -parameter-overrides DeployBucket=<bucket-name> DeployKey=<file-name> works properly

using Ref to fetch string value does not work

As soon as I use

"CodeUri" : {"Ref": "DeployString"}

changeset creation fails with message:

FAILED - Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless 
Application Specification document. Number of errors found: 1. Resource with
id [xxx] is invalid. 'CodeUri' requires Bucket and Key properties to be specified`

In this case I use aws deploy command without passing any parameters => stack creation should have used hardcoded parameter value from Parameters section.

Documentation states that usage of String or S3 location object is supported with CodeUri property. In this case it seems like Ref function can not be used to fetch string data for CodeUri property.

@sanathkr
Copy link
Contributor

Yes! This is how it is supposed to work. You cannot directly !Ref the string. Instead you should use the {Bucket, Key} notation - https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md#using-intrinsic-functions

SAM needs to parse CodeUri string into bucket/key, but unfortunately it cannot resolve runtime parameter values passed through !Ref.

@jikuja
Copy link
Author

jikuja commented Feb 2, 2018

You cannot directly !Ref the string. Instead you should use the {Bucket, Key} notation

Well. Worth of mention on documentation.

@zoltanhomoki
Copy link

Are there any plans to support this feature? It would be great to use the same Cloud Formation file both locally and when we deploy and passing the CodeUri as a CF parameter (locally the local folder/zip and the s3 bucket during deployment)

@msolimans
Copy link

@zoltanhomoki as a workaround, set CodeUri as local path and when you want to deploy, s3 bucket deploymemt, use sam package

@monkut
Copy link

monkut commented Apr 3, 2020

This is confusing since the documentation says you can use local file path:
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-codeuri

CodeUri
The Amazon S3 URI, local file path, or FunctionCode object of the function code.

If an Amazon S3 URI or FunctionCode object is provided, the Amazon S3 object referenced must be a valid Lambda deployment package.

If a local file path is provided, the template must go through the workflow that includes the sam deploy or sam package command, in order for the code to be transformed properly.

was using aws cloudformation create-stack command, will try with sam deploy

Despite the mention of using sam deploy for proper handling when local file path is specified this didn't work when using !Ref (might work with direct value?).

@lestephane
Copy link
Contributor

I was tripped up by this too. Whatever you think you did that warrants closing this issue, I can't see it.
The doc does not mention anything about this limitation.

@hoffa
Copy link
Contributor

hoffa commented Oct 17, 2022

Try adding AWS::LanguageExtensions to the Transforms as such:

Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

Haven't tested this specific use case, but AWS::LanguageExtensions resolves intrinsic functions out of the box (only ones that can be known before deployment). See #2533.

@hoffa
Copy link
Contributor

hoffa commented Nov 3, 2022

Closing in favor of #2533.

@hoffa hoffa closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/intrinsics Ref, If, Sub, GetAtt, ...
Projects
None yet
Development

No branches or pull requests

7 participants