AWS - SAM Create a generic role and attach to all lambdas -

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
Here, we will create an IAM role in the SAM template and attach it to lambda functions.
The type AWS::IAM::Role is used to define a role in SAM/cloud formation template, that typically contains IAM policies. An IAM policy can be AWS managed policy  AmazonSQSFullAccess or AmazonS3FullAccess , etc. or user-defined policies these are mostly for business-specific policies.
Resources:
  GenericLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaRole'
        - 'arn:aws:iam::aws:policy/AWSLambdaExecute'
        - 'arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess'
        - 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
        - 'arn:aws:iam::aws:policy/AmazonS3FullAccess'
        - 'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - 'lambda.amazonaws.com'
            Action:
              - 'sts:AssumeRole'
      Policies:
        - PolicyName: 'SecretsManagerParameterAccess'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ssm:GetParam*
                  - ssm:DescribeParam*
                Resource:
                  - arn:aws:ssm:*:*:parameter/
The above policy having six AWS managed policies and one user-defined policy that SecretsManagerParameterAccess. Now it can be attached to any number of lambda functions. Let’s use this policy to the Lambda function.
As we have seen in the earlier example, a lambda function can be attached with a role using Roleattribute.
Resources:
  SampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: sample-lambda-function
      Description: sample-lambda- lambda
      Role: !GetAtt GenericLambdaRole.Arn
      Handler: src.handle
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  Sample SAM Template

Resources:
  SampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: sample-lambda-function
      Description: sample-lambda- lambda
      Role: !GetAtt GenericLambdaRole.Arn
      Handler: src.handle

  SampleLambda2:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: sample2-lambda-function
      Description: sample2-lambda- lambda
      Role: !GetAtt GenericLambdaRole.Arn
      Handler: src.handle2

  GenericLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AWSLambdaRole'
        - 'arn:aws:iam::aws:policy/AWSLambdaExecute'
        - 'arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess'
        - 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
        - 'arn:aws:iam::aws:policy/AmazonS3FullAccess'
        - 'arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess'
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - 'lambda.amazonaws.com'
            Action:
              - 'sts:AssumeRole'
      Policies:
        - PolicyName: 'SecretsManagerParameterAccess'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - ssm:GetParam*
                  - ssm:DescribeParam*
                  - kms:GetSecretValue
                  - kms:Decrypt
                Resource:
                  - arn:aws:ssm:*:*:parameter/*
Done!
AWS – SAM serverless function
AWS IAM Roles
Happy Learning 🙂

AWS – SAM Serverless Function Example
AWS – SAM Lambda Layer example
Python – AWS SAM Lambda Example
Different ways to use Lambdas in Python
Java AWS – How to Create SQS Standard and FIFO Queues
[Fixed] – Malformed Lambda proxy response –  status 502
Javascript – How to listObjects from AWS S3
How to install AWS CLI on Windows 10
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
Java AWS – How to read messages from SQS
[Fixed] – Error: No changes to deploy. Stack is up to date
How add files to S3 Bucket using Shell Script
How to connect AWS EC2 Instance using PuTTY
Java AWS – How to Send Messages to SQS Queues

AWS – SAM Serverless Function Example
AWS – SAM Lambda Layer example
Python – AWS SAM Lambda Example
Different ways to use Lambdas in Python
Java AWS – How to Create SQS Standard and FIFO Queues
[Fixed] – Malformed Lambda proxy response –  status 502
Javascript – How to listObjects from AWS S3
How to install AWS CLI on Windows 10
How to Copy Local Files to AWS EC2 instance Manually ?
How set AWS Access Keys in Windows or Mac Environment
Java AWS – How to read messages from SQS
[Fixed] – Error: No changes to deploy. Stack is up to date
How add files to S3 Bucket using Shell Script
How to connect AWS EC2 Instance using PuTTY
Java AWS – How to Send Messages to SQS Queues
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows