How to attach an existing role to serverless.yml?

Viewed 7561

I want to attach an existing role to my serverless.yml file, I have created a role in aws console, my code works fine when I test it in aws console, but when I try to test it with the http endpoint it gives me the following:

{"message": "Internal server error"}

I think is because I did not specify any role in the serverless.yml file for the simple reason that I don't know how to do it.

Here is my serverless.yml file :


Resources: 
  ec2-dev-instance-status: 
    Properties: 
      Path: "arn:aws:iam::119906431229:role/lambda-ec2-describe-status"
      RoleName: lambda-ec2-describe-status
    Type: "AWS::IAM::Role"
functions: 
  instance-status: 
    description: "Status ec2 instances"
    events: 
      - 
        http: 
          method: get
          path: users/create
    handler: handler.instance_status
    role: "arn:aws:iam::119906431229:role/lambda-ec2-describe-status"
provider: 
  name: aws
  region: us-east-1
  runtime: python2.7
  stage: dev
resources: ~
service: ec2

Please help.

Thank you.

1 Answers

According to the documentation, there's a few ways to attach existing roles to a function (or entire stack)

Role defined as a Serverless resource

resources:
  Resources:
    myCustRole0:
      Type: AWS::IAM::Role
      # etc etc
functions:
  func0:
    role: myCustRole0

Role defined outside of the Serverless stack

functions:
  func0:
    role: arn:aws:iam::0123456789:role//my/default/path/roleInMyAccount

Note that the role you use must have additional permissions to log to cloudwatch etc, otherwise you won't get logging.

Related