Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added CloudFormation Custom Resource template to AWSLambdaPSCore PowerShell module templates #322

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# PowerShell script file to be executed as a AWS Lambda function.
#
# When executing in Lambda the following variables will be predefined.
# $LambdaInput - A PSObject that contains the Lambda function input data.
# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment.
#
# The last item in the PowerShell pipeline will be returned as the result of the Lambda function.
#
# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement
# indicating the module and version.

#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.343.0'}

# Uncomment to send the input event to CloudWatch Logs
# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5)

$CFNEvent = if ($null -ne $LambdaInput.Records) {
Write-Host 'Message received via SNS - Parsing out CloudFormation event'
$LambdaInput.Records[0].Sns.Message
}
else {
Write-Host 'Event received directly from CloudFormation'
$LambdaInput
}
$body = @{
# We'll assume success and overwrite if anything fails in line to avoid code duplication
Status = "SUCCESS"
Reason = "See the details in CloudWatch Log Stream:`n[Group] $($LambdaContext.LogGroupName)`n[Stream] $($LambdaContext.LogStreamName)"
PhysicalResourceId = $LambdaContext.LogStreamName
StackId = $CFNEvent.StackId
RequestId = $CFNEvent.RequestId
LogicalResourceId = $CFNEvent.LogicalResourceId
}
Write-Host "Processing RequestType [$($CFNEvent.RequestType)]"
try {
# If you want to return data back to CloudFormation, add the Data property to the body with the value as a hashtable. The hashtable keys will be the retrievable attributes when using Fn::GetAtt against the custom resource in your CloudFormation template:
# $body.Data = @{Secret = $null}
switch ($CFNEvent.RequestType) {
Create {
# Add Create request code here
}
Update {
# Add Update request code here
}
Delete {
# Add Delete request code here
}
}
}
catch {
Write-Error $_
$body.Status = "FAILED"
}
finally {
try {
Write-Host "Sending response back to CloudFormation"
Invoke-WebRequest -Uri $([Uri]$CFNEvent.ResponseURL) -Method Put -Body $($body | ConvertTo-Json -Depth 5)
}
catch {
Write-Error $_
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This sample creates a Lambda function written in PowerShell that processes custom
resource events from CloudFormation. It includes a Switch statement with placeholders
for the 3 different Request Types (Create, Update and Delete) that CloudFormation
sends. It also checks the event payload to see if CloudFormation delivered the event
via SNS (useful in case cross-account custom resources are in place) or if
CloudFormation sent the event directly to the Lambda. If the event is from SNS, it
will parse out the CloudFormation event information before processing the request type.

Once the event has been processed, it will send the results back to CloudFormation
via Invoke-WebRequest using the pre-signed URL sent with the original event.

The script contains a Requires statement for the latest version of the AWS Tools for
PowerShell module. If you modify this example to not need cmdlets from that
module you can safely delete this statement.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
}
]
},
{
"name": "CloudFormationCustomResource",
"description": "PowerShell handler base for use with CloudFormation custom resource events",
"content": [
{
"source": "cloudformationcustomresource.ps1.txt",
"output": "{basename}.ps1",
"filetype": "lambdaFunction"
},
{
"source": "readme.txt",
"output": "readme.txt"
}
]
},
{
"name": "CodeCommitTrigger",
"description": "Script to process AWS CodeCommit Triggers",
Expand Down