Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach an existing role to serverless.yml?

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.

like image 966
ner Avatar asked Sep 25 '17 12:09

ner


People also ask

How do I attach a role to Lambda?

Attach the IAM policy to an IAM roleNavigate to the IAM console and choose Roles in the navigation pane. Choose Create role. Choose AWS service and then choose Lambda. Choose Next: Permissions.

How do I pass an environment variable in serverless?

To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless. yml configuration file. It is valid to use the empty string in place of SOME_VAR . This looks like " ${env:} " and the result of declaring this in your serverless.

How do you refer a property in yml serverless?

To self-reference properties in serverless. yml , use the ${self:someProperty} syntax in your serverless.


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.

like image 193
Trent Bartlem Avatar answered Oct 26 '22 01:10

Trent Bartlem