Reputation: 669
I am creating a cloudformation template, with few resources, couple of lambda functions , S3 bucket.see code below, it is work on progress and so far I have a S3 bucket and a lamda function triggered by S3. we have vpc defined in our team that we are supposed to use. I would like to add private subnet under that vpc for my lambda function and assign public subnet for the s3 bucket. how to get reference of the vpc , and pass it to my template and use it? sample code will be helpful.
AWSTemplateFormatVersion: 2010-09-09
Resources:
# S3 Bucket
S3Bucket:
Type: AWS::S3::Bucket
# Functions
S3-Lambda-trigger:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: lambda.handler
Description: s3 object creation triggers lambda
Runtime: nodejs12.x
Events:
S3Bucket:
Type: S3
Properties:
Bucket: !Ref S3Bucket
Events: 's3:ObjectCreated:*'
# Permissions
Allow-lamda-invocation-s3:
Type: AWS::Lambda::Permission
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !Ref S3-Lambda-trigger
Principal: s3.amazonaws.com
SourceArn: !GetAtt S3Bucket.Arn
Upvotes: 1
Views: 2561
Reputation: 238081
how to get reference of the vpc , and pass it to my template and use it?
One way would be through AWS-Specific Parameter Types, specifically AWS::EC2::VPC::Id
, in a Parameters
section.
For example:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
VPCId:
Type: AWS::EC2::VPC::Id
Resources:
MySubnet:
Type: AWS::EC2::Subnet
Properties:
# other properties
VpcId: !Ref VPCId
Thanks to this, when creating the stack in AWS Console, you would be able to choose existing VPCId to pass to the template.
Upvotes: 1