Teodoro
Teodoro

Reputation: 1474

How can I bind my existing programs to Pulumi?

I'm starting to work with Pulumi for IaC design of my project but I'm having a hard time in understanding how to bind my existing code to the use of Pulumi.
For example, suppose I've made a lambda function in python with the following content:

# test_lambda.py

import boto3
import json


sqs_client = boto3.client('sqs')
ssm_client = boto3.client('ssm')


def get_auth_token():
    response = ssm_client.get_parameters(
        Names=[
            'lambda_auth_token',
        ],
        WithDecryption=False
    )
    return response["Parameters"][0]["Value"]


def handler(event, _):
    body = json.loads(event['body'])
    if body['auth_token'] == get_auth_token():    
        sqs_client.send_message(
            QueueUrl='my-queue',
            MessageBody='validated auth code',
            MessageDeduplicationId='akjseh3278y7iuad'
        )
        return {'statusCode': 200}
    else:
        return {'statusCode': 403}

How do I reference this whole file containing the lambda function in a Pulumi project? So I could then use this lambda integrated with SNS service.

And also, since I'm using Pulumi for my architecture, boto3 seems uneeded, I could just replace it by Pulumi aws library right? And then the python interpreter will just use Pulumi as a common interface library for my aws resources (like boto3)? This last question might seem odd but for now I've only seen the use of pulumi as the stack and architecture "builder" when running pulumi up.

Upvotes: 1

Views: 312

Answers (1)

zXor
zXor

Reputation: 218

You could try importing the lambda into your pulumi stack like below

aws.lambda_.Function("sqs_lambda", opts=ResourceOptions(import=[
    "lambda_id",
]))

A pulumi up , post this will mean the lambda is managed by pulumi from there on.You should also be cautious of

pulumi stack destroy

as that would mean the imported resources are also deleted. You can read Pulumi import resource

Upvotes: 1

Related Questions