Skip to content

Use variable in layers #751

Closed
Closed

Description

@mnapoli

Description:

We are building PHP runtimes (as layers) in Bref.

But to use the layers users have to replace the region in template.yaml with the region their application is in.

Similar problem: the version of the layer will change over time, users will have to manually edit template.yaml to change the version number.

Steps to reproduce the issue:

  1. Create the following template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
    MyFunction:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: 'my-function'
            CodeUri: .
            Handler: index.php # the name of your PHP file
            Runtime: provided
            Layers:
                - 'arn:aws:lambda:${AWS_DEFAULT_REGION}:416566615250:layer:php-72:${MY_LAYER_VERSION}'
  1. Try to deploy or run locally

Observed result:

It doesn't work.

Expected result:

It should replace the variables in the layer name with the existing environment variables.

Activity

  1. timoschilling commented on Jan 9, 2019

    @timoschilling
    Contributor

    have you tried this?

     - !Sub 'arn:aws:lambda:${AWS_DEFAULT_REGION}:416566615250:layer:php-72:${MY_LAYER_VERSION}'
    
  2. mnapoli commented on Jan 10, 2019

    @mnapoli
    Author

    @timoschilling yes it doesn't either

    - 'arn:aws:lambda:us-east-2:416566615250:layer:php-72:9'

    works.

    - !Sub 'arn:aws:lambda:us-east-2:416566615250:layer:php-72:${MY_LAYER_VERSION}'

    doesn't work, this is the result in sam local:

    {
      "errorType": "Runtime.ExitError",
      "errorMessage": "RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]"
    }

    Also this is the result when trying to deploy with SAM:

    Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Template format error: Unresolved resource dependencies [MY_LAYER_VERSION] in the Resources block of the template
    

    - !Sub 'arn:aws:lambda:us-east-2:416566615250:layer:php-72:9'

    doesn't work either with sam local.

    I think this is related to this issue: aws/aws-sam-cli#528

  3. keetonian commented on Jan 18, 2019

    @keetonian
    Contributor

    There seem to be a few issues:

    1. AWS_DEFAULT_REGION is not a CFN parameter, but could be user defined. The corresponding CFN parameter is AWS::Region
    2. MY_LAYER_VERSION wasn't defined in the parameters section of the template you supplied.
    3. You need to use !Sub in order to resolve these parameters
    4. SAM CLI doesn't resolve many intrinsics; this may be an intrinsic that it can't (yet) resolve and so it returns an error.

    The following template should work (provided that the layer arn is valid):

    AWSTemplateFormatVersion: '2010-09-09'
    Transform: 'AWS::Serverless-2016-10-31'
    Parameters:
      MyLayerVersion:
        Default: 1
        Type: Number
    Resources:
        MyFunction:
            Type: AWS::Serverless::Function
            Properties:
                FunctionName: 'my-function'
                CodeUri: .
                Handler: index.php # the name of your PHP file
                Runtime: provided
                Layers:
                    - !Sub 'arn:aws:lambda:${AWS::Region}:416566615250:layer:php-72:${MyLayerVersion}'
  4. mnapoli commented on Jan 19, 2019

    @mnapoli
    Author

    Thank you so much!

    It works when deploying to AWS, however it doesn't work with sam local. I guess this is now an issue to follow at aws/aws-sam-cli#528

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions