serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Support CloudFormation intrinsic functions in SAM resources

Open sanathkr opened this issue 10 years ago • 37 comments

We quite often use !Ref, !Sub, !Join in properties that are supposed to hold strings such as CodeUri, DefinitionUri, StageName (#19) etc. This is a perfectly valid CloudFormation, but the transformation logic that parses SAM does not resolve intrinsic functions before parsing the property as a string. So the transformation logic would fail and depending on the property it could either return a validation error or internal failure. Properties that are directly passed down to CloudFormation are unaffected by this problem.

This is a feature request to add support for resolving a subset of CloudFormation intrinsics in the transformation logic. We care about only the subset of properties that the transform manipulates, like CodeUri.

Following intrinsics will be supported:

  • !Sub
  • !Join

on following properties:

  • CodeUri on AWS::Serverless::Function
  • Role on AWS::Serverless::Function
  • DefinitionUri on AWS::Serverless::Api
  • StageName on AWS::Serverless::Api

sanathkr avatar Nov 23 '16 23:11 sanathkr

Trying to use !GetAtt for SecurityGroupIds in a VpcConfig section of a Lambda, get Type of property 'VpcConfig' is invalid.

Works fine when I manually look up the values and plug in the strings, so figure it must be this same issue.

Timeframe on a fix? Any workarounds possible?

Loving the new SAM stuff but this is a deal-breaker.

deathbob avatar Nov 29 '16 14:11 deathbob

I am sorry you are running into the bug. Unfortunately there are no workarounds besides switching to regular AWS::Lambda::Function resource.

We are actively working on fixing it. Will let you know once its done

sanathkr avatar Nov 29 '16 20:11 sanathkr

I've been having this issue with Fn::ImportValue for the ::Api Variables property to be able to pass in the function arn.

Is this bug fix to support all of the intrinsic functions?

stuartstevenson avatar Dec 15 '16 16:12 stuartstevenson

@stuartstevenson #20 was a bug that was fixed. You should be able to use any Fn:: function with Variables property. However you cannot pass the ARN directly to your Swagger. It doesn't work. You should pass function name and construct the ARN there. See this example: https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/swagger.yaml#L23

sanathkr avatar Dec 15 '16 19:12 sanathkr

Fn::Import wasn't working for.me earlier today. Can you all confirm that it is fixed? I can create an example if necessary.

joekiller avatar Dec 15 '16 21:12 joekiller

Can you give me an example with the error you received?

sanathkr avatar Dec 15 '16 22:12 sanathkr

I'll create one tomorrow and post here.

joekiller avatar Dec 15 '16 22:12 joekiller

@sanathkr Thank you for your response. Is there a reason why ARNs in particular cannot be passed as I had made the assumption that the stage variable was just a string i was setting? I would not have assumed that the contents of the string would be validated.

stuartstevenson avatar Dec 16 '16 12:12 stuartstevenson

I was using multiple short form functions in a row which is not supported. So trying

Resource: !ImportValue !Sub '${KMSKey}-blah'

Is improper while

Resource: !ImportValue 
                      'Fn::Sub': '${KMSKey}-blah'

Works. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html Notes the problem I had.

joekiller avatar Dec 16 '16 18:12 joekiller

I'm still getting this error when trying to use !ImportValue for AWS::Serverless::Function


Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: bundle.handler
      MemorySize: 128
      Runtime: nodejs4.3
      Timeout: 3
      Events:
        List:
          Type: Api
          Properties:
            RestApiId: !ImportValue API-ApiId
            Path: /users
            Method: GET

brettstack avatar Dec 21 '16 04:12 brettstack

I'm experiencing a variation of this trying to use !Ref as a value in a Function's Environment. Is my only option to switch to AWS::Lambda::Function for now?

Edit: I had a typo in my CodeUri attribute and that caused "internal error in transform" which led me here. Sorry.

weargoggles avatar Jan 19 '17 13:01 weargoggles

!Ref in environment variables is supposed to work out-of-box. Can you post an example?

sanathkr avatar Jan 25 '17 19:01 sanathkr

It did work. I was mistaken.

weargoggles avatar Jan 25 '17 20:01 weargoggles

Our org would also like to see !Ref supported in a the CodeUri field of a AWS::Serverless::Function in addition to the proposed items in the original comment.

Something like this, is what we are thinking (idolized design):

      CodeUri:
        Fn::Join:
          - '-'
          - - s3://some-known-convention
            - Ref: AWS::AccountId
            - '/github-sha/deployment.zip'

jolexa avatar Feb 09 '17 17:02 jolexa

Thanks for the comment. Yes, we will support this usecase as well...

sanathkr avatar Feb 09 '17 18:02 sanathkr

Interestingly, many of the intrinsics work through CodePipeline action for CloudFormation. Is that case handled differently?

dinvlad avatar Feb 16 '17 04:02 dinvlad

We have just pushed support for Intrinsic functions - https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md#using-intrinsic-functions

It should work everywhere except for the caveats mentioned in the above doc. If you find any scenario where it doesn't work, please create a new bug.

!Sub "Thanks everyone for the awesome feedback. ${your_username} are awesome!"

sanathkr avatar Feb 17 '17 22:02 sanathkr

Just curious, when is this available? Is it currently available in AWS::Serverless-2016-10-31?

judahb avatar Sep 25 '17 17:09 judahb

Should all be available.

-Joe Sent via mobile.

On Sep 25, 2017 1:52 PM, "Judah Bernstein" [email protected] wrote:

Just curious, when is this available? Is it currently available in AWS::Serverless-2016-10-31?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/awslabs/serverless-application-model/issues/22#issuecomment-331960710, or mute the thread https://github.com/notifications/unsubscribe-auth/AA-bx4hu56z2sGAJrkkMMihw5r1ot1yoks5sl-hpgaJpZM4K7Jk_ .

joekiller avatar Sep 25 '17 22:09 joekiller

I am trying to do something like this to enable both local and CI run but doesn't work: CodeUri: !If - CreateDevEnvironment - ../../../target/helloworld-1.0-SNAPSHOT.jar - Bucket: !Ref BucketName Key: !Ref CodeKey Is If intrinsic function supported? Or how I can switch between 2 different objects (string and S3 location object) for CodeUrl in the same template?

baechul avatar Dec 23 '17 04:12 baechul