Home > Software design >  Referencing an AWS Lambda's role inside a SAM Template (CF Stack) YAML
Referencing an AWS Lambda's role inside a SAM Template (CF Stack) YAML

Time:12-31

I have an AWS SAM template defining, amongst many other things, a JavaScript Lambda:

Resources:
  notesFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Zip
      CodeUri: notes/
      Handler: app.lambdaHandler
      Runtime: nodejs18.x
      Policies:
      - AmazonDynamoDBFullAccess
      Architectures:
        - x86_64
      Events:
        Fetchnotes:
          Type: Api
          Properties:
            Path: /notes
            Method: get
        Givenotes:
          Type: Api
          Properties:
            Path: /notes
            Method: post
        Users:
          Type: Api
          Properties:
            Path: /notes/users
            Method: get
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints:
        - app.ts

Later on in the template, I am trying to reference this Lambda's role (example: Role: !Ref <MyLambdaRole>) but not sure how to do that, since the role is created on the fly when deploying the SAM template (CloudFormation stack). Any ideas how I can do this?

CodePudding user response:

If you do not provide a role in your AWS::Serverless::Function definition, SAM creates a role with a Logical ID of <function‑LogicalId>Role.

In your case, this would be !Ref notesFunctionRole.

  • Related