AWS Serverless: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 28: Line 28:
<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
{
{
  "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
  "Resources": {
        "cowsayIamRole": {
      "RootRole": {
            "Type": "AWS::IAM::Role",
        "Type": "AWS::IAM::Role",
            "Properties": {
        "Properties": {
                "AssumeRolePolicyDocument": {
            "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
              "Version" : "2012-10-17",
                    "Statement": [
              "Statement": [ {
                        {
                  "Effect": "Allow",
                            "Effect": "Allow",
                  "Principal": {
                            "Principal": {
                    "Service": [ "ec2.amazonaws.com" ]
                                "Service": [
                  },
                                    "lambda.amazonaws.com"
                  "Action": [ "sts:AssumeRole" ]
                                ]
              } ]
                            },
            },
                            "Action": [
            "Path": "/"
                                "sts:AssumeRole"
        }
                            ]
      },
                        }
      "RolePolicies": {
                    ]
        "Type": "AWS::IAM::Policy",
                },
        "Properties": {
                "Path": "/",
            "PolicyName": "root",
                "Policies": [
            "PolicyDocument": {
                    {
              "Version" : "2012-10-17",
                        "PolicyName": "root",
              "Statement": [ {
                        "PolicyDocument": {
                  "Effect": "Allow",
                            "Version": "2012-10-17",
                  "Action": "*",
                            "Statement": [
                  "Resource": "*"
                                {
              } ]
                                    "Effect": "Allow",
            },
                                    "Action": [
            "Roles": [ {
                                        "logs:*"
              "Ref": "RootRole"
                                    ],
            } ]
                                    "Resource": "arn:aws:logs:*:*:*"
        }
                                }
      },
                            ]
      "RootInstanceProfile": {
                        }
        "Type": "AWS::IAM::InstanceProfile",
                    }
        "Properties": {
                ]
            "Path": "/",
            }
            "Roles": [ {
        }
              "Ref": "RootRole"
    }
            } ]
}</syntaxhighlight>
        }
      }
  }
}
</syntaxhighlight>


==Lambda Function==
==Lambda Function==

Revision as of 00:05, 19 February 2022

Introduction

This is an example of how to set up a serverless framework function within AWS

Setup

Within AWS there are example templates for each piece of the infrastructure. For this we need to create a

  • S3 Bucket to hold the code
  • IAM::Role to describe the permissions
  • Lambda The function to run

Templates

I have copied the example templates for each component and highlighted the ones we need to change

S3 Bucket

Example can be found here

{
    "Resources": {
        "cowsayBucket": {
            "Type": "AWS::S3::Bucket",
            # "DeletionPolicy": "Retain"
            "Properties": {
                "BucketName": "cowsayBucket"
            }
        }
    }
}

IAM::Role

Example can be found here

{
    "Resources": {
        "cowsayIamRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {
                                "Service": [
                                    "lambda.amazonaws.com"
                                ]
                            },
                            "Action": [
                                "sts:AssumeRole"
                            ]
                        }
                    ]
                },
                "Path": "/",
                "Policies": [
                    {
                        "PolicyName": "root",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": [
                                        "logs:*"
                                    ],
                                    "Resource": "arn:aws:logs:*:*:*"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

Lambda Function

Example can be found here

"AMIIDLookup": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
        "Handler": "index.handler",
        "Role": {
            "Fn::GetAtt": [
                "LambdaExecutionRole",
                "Arn"
            ]
        },
        "Code": {
            "S3Bucket": "lambda-functions",
            "S3Key": "amilookup.zip"
        },
        "Runtime": "nodejs12.x",
        "Timeout": 25,
        "TracingConfig": {
            "Mode": "Active"
        }
    }
}