21

Considering this lambda function on a serverless.yml file:

functions:
  s3toEc2Lambda:
    handler: s3toec2lambda.S3toEc2Lambda
    name: "${self:service}-s3toEc2Lambda"
    role: S3toEc2LambdaRole

And considering this SNS created on resources section: Does someone knows how to inform the Sns ARN Endpoint from the lambda function s3toEc2Lambda ?

resources:
  Resources:
    WordpressFrontEndSnsS3toEc2:
      Type: AWS::SNS::Topic
      Properties:
        TopicName: "wordpress-front-end-s3-ec2"

    WordpressFrontEndSnsS3toEc2Lambda:
      Type: AWS::SNS::Subscription
      Properties:
        Endpoint: { "Fn::GetAtt": ["s3toEc2Lambda", "Arn" ] }                    <------ HERE    <------
        #Endpoint: ${self:functions.s3toEc2Lambda}                               <------ OR HERE <------
        #Endpoint: { "Fn::GetAtt": ["${self:functions.s3toEc2Lambda}", "Arn" ] } <------ OR HERE <------
        Protocol: lambda
        TopicArn: !Ref 'WordpressFrontEndSnsS3toEc2'

For me always appear a error message like this: "Template error: instance of Fn::GetAtt references undefined resource s3toEc2Lambda"

Thank You !

2 Answers2

23

CloudFormation resources created by serverless have known format. For lambda function this is:

{normalizedFunctionName}LambdaFunction

Thus you should be able to reference your function using the following:

"Fn::GetAtt": [ S3toEc2LambdaLambdaFunction, Arn ]

More example about this are here

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • what if the resource in within a different service – Diego Ponciano Jan 24 '22 at 23:15
  • 1
    For some reason, it doesn't work if the name of the lambda function is s3to-ec2-lambda, I had to change it to s3toEc2Lambda – Yao Jun 16 '22 at 13:42
  • 1
    This should be marked as the answer, look at the link and make sure you're referencing the function's name correctly. Capitalisation and formatting do matter! – Cai Allin Jul 20 '23 at 09:00
0

We can create Function Roles, Functions Policy and Lambda functions SAM template.yml file by this

    Type: AWS::IAM::Role
    Properties:
      RoleName: UatAdminUserStatsLambda
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Principal:
              Service:
                - 'lambda.amazonaws.com'
            Action:
              - 'sts:AssumeRole'

  FunctionPolicy:
    Type: AWS::IAM::Policy
    DependsOn: FunctionRole
    Properties:
      PolicyName: UserStatsPolicy
      Roles:
        - !Ref FunctionRole
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Action:
              - 'iam:GetUser'
              - 'logs:CreateLogGroup'
              - 'logs:CreateLogStream'
              - 'logs:GetLogEvents'
              - 'logs:PutLogEvents'
              - 's3:GetObject'
              - 's3:PutObject'
            Resource: '*'

  adminUsersList:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/admin/
      Role: !GetAtt FunctionRole.Arn
      Handler: adminUsersList.adminUsersList
      Layers:
        - !Ref NodeDependenciesLayer
      Events:
        adminUsersListEvent:
          Type: Api
          Properties:
            Path: /api/admins
            Method: GET
Mohsin
  • 19
  • 5