Implement manual approval confirmation email from 2 people

0

Need to implement a framework to send approval email for two persons once we received approval then only need to take snapshot in boto3 and proceeding further Could you please help me how to send multi approval email and get the both confirmation for further task

1개 답변
0

⚡I'm updating my response because I missed the tag for Step Function, Lambda, and SNS, and didn't consider using serverless services.


Detailed Architecture Diagram Description

Architecture Diagram Description

Here's a basic flow:

  1. External Event trigger the workflow in AWS Step Functions.
  2. Lambda function sends an approval request via SES.
  3. Lambda function checks DynamoDB for approvals.
  4. Once approvals are confirmed, another Lambda function performs the snapshot.
  5. Workflow completes.

ℹ️ You would need to write AWS Lambda functions for several parts of the process: sending the approval requests, handling the approval reception, checking if both approvals have been received, and taking a snapshot.

Send Approval Requests Lambda Function

import boto3
from urllib.parse import quote_plus

def generate_approval_url(approval_lambda_url, user_id):
    return f"{approval_lambda_url}?response=approve&user_id={quote_plus(user_id)}"

def lambda_handler(event, context):
    ses = boto3.client('ses')
    approval_lambda_url = 'https://your-api-gateway-url'

    approvers = {
        'User1': 'user1@example.com',
        'User2': 'user2@example.com',
    }
    
    for user_id, email in approvers.items():
        approval_url = generate_approval_url(approval_lambda_url, user_id)
        ses.send_email(
            Source='sender@example.com',
            Destination={'ToAddresses': [email]},
            Message={
                'Subject': {'Data': 'Approval Request'},
                'Body': {
                    'Text': {
                        'Data': f'Please approve the request by clicking on this link: {approval_url}'
                    }
                }
            }
        )
    return {'status': 'Emails sent'}

Approval Reception Lambda Function

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('ApprovalsTable')

    user_id = event['queryStringParameters']['user_id']
    response = event['queryStringParameters']['response']

    table.update_item(
        Key={'UserId': user_id},
        UpdateExpression='SET ApprovalStatus = :val1',
        ExpressionAttributeValues={':val1': response}
    )

    return {
        'statusCode': 200,
        'body': 'Approval received'
    }

Check Approvals Lambda Function

import boto3

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('ApprovalsTable')

    response = table.scan(
        FilterExpression='ApprovalStatus = :val',
        ExpressionAttributeValues={':val': 'approve'}
    )

    if response['Count'] == 2:
        return {'status': 'approved'}
    else:
        return {'status': 'not_approved'}

Snapshot Lambda Function

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    
    instance_id = 'i-1234567890abcdef0'

    snapshot = ec2.create_snapshot(InstanceId=instance_id, Description='Snapshot after approval')
    
    return {'status': 'Snapshot created', 'snapshot_id': snapshot['SnapshotId']}

ℹ️ Don't forget to handle permissions and environment variables appropriately. Each Lambda function will need the correct IAM permissions to interact with SES, DynamoDB, EC2, and other services as required.

profile picture
전문가
답변함 한 달 전
  • The requester tagged the question with Step Functions and I don't see any workflows in the answer. Additionally, it is unclear how the approvals are collected from the approvers.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠