Pulumi
Pulumi is an open-source infrastructure as code SDK that allows you to define and deploy your cloud resources using a programming language of your choice. It's possible to define any kind of cloud resource, but given this is serverlessdotnet.dev let's build an AWS Lambda function.
The first thing you'll need to do is install Pulumi..
Once installed, create a new directory and initialize a project:
Take note of the stack name used in the pulumi new
wizard, you'll need this for deployment.
Let's also create a new Lambda function to use as an example.
cd ..mkdir Functionscd Functionsdotnet new lambda.EmptyFunction -n Dotnet.CDK.Lambda
Open up the generated project file called PulumiDotnet.Serverless.csproj
.
Replace the Program.cs
file with the below code:
Then add an additional class to the project named LambdaFunctionStack
.
Update the contents with the below code:
Click Here for Code Sample
IAM Role
The first thing defined is an IAM role for the Lambda function to use. The role simply allows the Lambda service to assume it.
IAM Policy
Next, an IAM policy is defined and attached to the role using the RolePolicy
class.
Lambda Function
Finally, the Lambda function is defined and the name of it set to the Output name. Pulumi provides the FileArchive
class to specify the files to be included in the ZIP and upload it to Amazon S3.
IAM Role
The first thing defined is an IAM role for the Lambda function to use. The role simply allows the Lambda service to assume it.
IAM Policy
Next, an IAM policy is defined and attached to the role using the RolePolicy
class.
Lambda Function
Finally, the Lambda function is defined and the name of it set to the Output name. Pulumi provides the FileArchive
class to specify the files to be included in the ZIP and upload it to Amazon S3.
Once defined, run the below commands from a terminal window in the PulumiServerless
folder.
Once deployed, invoke the Lambda function in AWS using the below command replacing the OUTPUT_FUNCTION_NAME
with the value output to your terminal:
dotnet lambda invoke-function -n OUTPUT_FUNCTION_NAME -p hello