Skip to content

Instantly share code, notes, and snippets.

@cdechery
Created November 28, 2022 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdechery/86d39c1b0863578fcd267a23741cf4a9 to your computer and use it in GitHub Desktop.
Save cdechery/86d39c1b0863578fcd267a23741cf4a9 to your computer and use it in GitHub Desktop.
Having trouble creating Lambdas due to proxy related issues? This template might help.
########
# Deploys a "Hello World" Lambda Function
# @createdby Christian Dechery
# @date 25-nov-2022
########
AWSTemplateFormatVersion: "2010-09-09"
Description: Deploys a very basic "Hello World" Lambda function
Parameters:
UseVPC:
Type: String
AllowedValues:
- "yes"
- "no"
Default: "no"
VpcId:
Type: AWS::EC2::VPC::Id
Description: Select a VPC to launch EC2 instances (if UseVPC=yes)
SubnetIDs:
Type: List<AWS::EC2::Subnet::Id>
Description: Select two private subnets to host the server (if UseVPC=yes)
Runtime:
Type: String
Description: Select the runtime of your function (default = python3.8)
AllowedValues:
- dotnet6
- dotnetcore3.1
- go1.x
- java11
- java8
- java8.al2
- nodejs12.x
- nodejs14.x
- nodejs16.x
- provided
- provided.al2
- python3.7
- python3.8
- python3.9
- ruby2.7
Default: python3.8
Conditions:
WithVPC: !Equals [!Ref UseVPC, "yes"]
NoVPC: !Equals [!Ref UseVPC, "no"]
Resources:
LambdaSecurityGroup:
Condition: WithVPC
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Sandbox Lambda security group (egress = all)
VpcId: !Ref VpcId
LambdaSecurityGroupOutbound:
Condition: WithVPC
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !Ref LambdaSecurityGroup
IpProtocol: -1
CidrIp: 0.0.0.0/0
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Description: "Give access to Lambda do execute"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
LambdaFunctionWithVPC:
Condition: WithVPC
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import os
import json
def lambda_handler(event, context):
json_region = os.environ['AWS_REGION']
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Region ": json_region
})
}
Runtime: !Ref Runtime
Handler: lambda_function.lambda_handler
VpcConfig:
SecurityGroupIds:
- !Ref LambdaSecurityGroup
SubnetIds: !Ref SubnetIDs
LambdaFunctionNoVPC:
Type: AWS::Lambda::Function
Condition: NoVPC
Properties:
Role: !GetAtt LambdaRole.Arn
Code:
ZipFile: |
import os
import json
def lambda_handler(event, context):
json_region = os.environ['AWS_REGION']
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"Region ": json_region
})
}
Runtime: !Ref Runtime
Handler: lambda_function.lambda_handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment