python code to integrate with Lambda & API Gateway using boto3 🛠️🔗
Amazing Task :
These article will goes to tells the crazy code to integrates lambda and api gateway using boto3.
Now I will tell you 8 different amazon services , How to the code is and how to integrate with lambda and api gateway using boto3.
Amazon Service List :
1️⃣ Amazon Polly
2️⃣ Amazon Rekognition
3️⃣ Amazon Comprehend
4️⃣ Amazon Transcribe
5️⃣ Amazon Translate
6️⃣ Amazon Personalize
7️⃣ Amazon MediaLive
8️⃣ Amazon MediaConvert
1️⃣ Amazon Polly:
Simple, First we need to install the boto3 package,
import boto
# Initialize AWS clients
lambda_client = boto3.client('lambda')
apigateway_client = boto3.client('apigateway')
# Create a Lambda function
lambda_function_name = 'PollySynthesizeSpeech'
lambda_role_arn = 'arn:aws:iam::YOUR_ACCOUNT_ID:role/your-lambda-role' # Update with your Lambda role ARN
lambda_create_response = lambda_client.create_function(
FunctionName=lambda_function_name,
Runtime='python3.8',
Role=lambda_role_arn,
Handler='lambda_function.lambda_handler', # Assuming the handler function is named lambda_handler
Code={
'S3Bucket': 'your-bucket-name',
'S3Key': 'lambda-code.zip' # Update with your code's S3 bucket and key
}
)
lambda_function_arn = lambda_create_response['FunctionArn']
# Create API Gateway
api_name = 'PollyAPI'
api_create_response = apigateway_client.create_rest_api(name=api_name)
api_id = api_create_response['id']
# Create a resource
resource_create_response = apigateway_client.create_resource(
restApiId=api_id,
parentId=None,
pathPart='synthesize'
)
resource_id = resource_create_response['id']
# Create a POST method for the resource
method_create_response = apigateway_client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod='POST',
authorizationType='NONE'
)
# Integrate the method with the Lambda function
integration_create_response = apigateway_client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='POST',
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=f'arn:aws:apigateway:{REGION}:lambda:path/2015-03-31/functions/{lambda_function_arn}/invocations'
)
# Deploy the API
deployment_create_response = apigateway_client.create_deployment(
restApiId=api_id,
stageName='prod'
)
# Generate the API Gateway endpoint URL
api_endpoint = f'https://{api_id}.execute-api.{REGION}.amazonaws.com/prod/synthesize'
print(f"API Gateway endpoint URL: {api_endpoint}")
2️⃣ Amazon Rekognition :
import boto
# Initialize AWS clients
lambda_client = boto3.client('lambda')
apigateway_client = boto3.client('apigateway')
# Create a Lambda function
lambda_function_name = 'RekognitionImageAnalysis'
lambda_role_arn = 'arn:aws:iam::YOUR_ACCOUNT_ID:role/your-lambda-role' # Update with your Lambda role ARN
lambda_create_response = lambda_client.create_function(
FunctionName=lambda_function_name,
Runtime='python3.8',
Role=lambda_role_arn,
Handler='lambda_function.lambda_handler', # Assuming the handler function is named lambda_handler
Code={
'S3Bucket': 'your-bucket-name',
'S3Key': 'lambda-code.zip' # Update with your code's S3 bucket and key
}
)
lambda_function_arn = lambda_create_response['FunctionArn']
# Create API Gateway
api_name = 'RekognitionAPI'
api_create_response = apigateway_client.create_rest_api(name=api_name)
api_id = api_create_response['id']
# Create a resource
resource_create_response = apigateway_client.create_resource(
restApiId=api_id,
parentId=None,
pathPart='analyze'
)
resource_id = resource_create_response['id']
# Create a POST method for the resource
method_create_response = apigateway_client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod='POST',
authorizationType='NONE'
)
# Integrate the method with the Lambda function
integration_create_response = apigateway_client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='POST',
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=f'arn:aws:apigateway:{REGION}:lambda:path/2015-03-31/functions/{lambda_function_arn}/invocations'
)
# Deploy the API
deployment_create_response = apigateway_client.create_deployment(
restApiId=api_id,
stageName='prod'
)
# Generate the API Gateway endpoint URL
api_endpoint = f'https://{api_id}.execute-api.{REGION}.amazonaws.com/prod/analyze'
print(f"API Gateway endpoint URL: {api_endpoint}")
Remember to replace placeholders like YOUR_ACCOUNT_ID, your-lambda-role, your-bucket-name, lambda-code.zip, and REGION with your actual values.
This version condenses the code while maintaining the core functionalities. As always, adapt the code to your specific needs and add proper error handling and security considerations.
Finally , For all the packages
import boto
# Initialize AWS clients
lambda_client = boto3.client('lambda')
apigateway_client = boto3.client('apigateway')
# Function to create Lambda function
def create_lambda_function(function_name, role_arn, handler, code_s3_bucket, code_s3_key):
return lambda_client.create_function(
FunctionName=function_name,
Runtime='python3.8',
Role=role_arn,
Handler=handler,
Code={
'S3Bucket': code_s3_bucket,
'S3Key': code_s3_key
}
)['FunctionArn']
# Function to create API Gateway resource and method
def create_api_resource_and_method(api_id, path_part, http_method):
resource_id = apigateway_client.create_resource(
restApiId=api_id,
parentId=None,
pathPart=path_part
)['id']
apigateway_client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod=http_method,
authorizationType='NONE'
)
return resource_id
# Function to integrate method with Lambda
def integrate_method_with_lambda(api_id, resource_id, http_method, integration_uri):
apigateway_client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod=http_method,
integrationHttpMethod='POST',
type='AWS_PROXY',
uri=integration_uri
)
# Function to create API deployment
def create_api_deployment(api_id):
apigateway_client.create_deployment(
restApiId=api_id,
stageName='prod'
)
# Function to generate API Gateway endpoint URL
def generate_api_endpoint(api_id):
return f'https://{api_id}.execute-api.{REGION}.amazonaws.com/prod/'
# Define your AWS region
REGION = 'your-aws-region'
# Define your Lambda role ARN
LAMBDA_ROLE_ARN = 'arn:aws:iam::YOUR_ACCOUNT_ID:role/your-lambda-role'
# Create a Lambda function for each service
comprehend_function_arn = create_lambda_function(
'ComprehendAnalysis',
LAMBDA_ROLE_ARN,
'lambda_function.lambda_handler',
'your-bucket-name',
'lambda-code.zip'
)
transcribe_function_arn = create_lambda_function(
'TranscribeProcessing',
LAMBDA_ROLE_ARN,
'lambda_function.lambda_handler',
'your-bucket-name',
'lambda-code.zip'
)
translate_function_arn = create_lambda_function(
'TranslateTranslation',
LAMBDA_ROLE_ARN,
'lambda_function.lambda_handler',
'your-bucket-name',
'lambda-code.zip'
)
# ... Repeat for other services
# Create API Gateway for each service
comprehend_api_id = apigateway_client.create_rest_api(name='ComprehendAPI')['id']
transcribe_api_id = apigateway_client.create_rest_api(name='TranscribeAPI')['id']
translate_api_id = apigateway_client.create_rest_api(name='TranslateAPI')['id']
# ... Repeat for other services
# Create resources and methods for each service
comprehend_resource_id = create_api_resource_and_method(comprehend_api_id, 'analyze', 'POST')
transcribe_resource_id = create_api_resource_and_method(transcribe_api_id, 'transcribe', 'POST')
translate_resource_id = create_api_resource_and_method(translate_api_id, 'translate', 'POST')
# ... Repeat for other services
# Integrate methods with Lambda functions for each service
integrate_method_with_lambda(comprehend_api_id, comprehend_resource_id, 'POST', f'arn:aws:apigateway:{REGION}:lambda:path/2015-03-31/functions/{comprehend_function_arn}/invocations')
integrate_method_with_lambda(transcribe_api_id, transcribe_resource_id, 'POST', f'arn:aws:apigateway:{REGION}:lambda:path/2015-03-31/functions/{transcribe_function_arn}/invocations')
integrate_method_with_lambda(translate_api_id, translate_resource_id, 'POST', f'arn:aws:apigateway:{REGION}:lambda:path/2015-03-31/functions/{translate_function_arn}/invocations')
# ... Repeat for other services
# Create API deployments for each service
create_api_deployment(comprehend_api_id)
create_api_deployment(transcribe_api_id)
create_api_deployment(translate_api_id)
# Generate API Gateway endpoint URLs for each service
comprehend_api_endpoint = generate_api_endpoint(comprehend_api_id)
transcribe_api_endpoint = generate_api_endpoint(transcribe_api_id)
translate_api_endpoint = generate_api_endpoint(translate_api_id)
# ... Repeat for other services
# Print API Gateway endpoint URLs for each service
print(f"Comprehend API endpoint URL: {comprehend_api_endpoint}")
print(f"Transcribe API endpoint URL: {transcribe_api_endpoint}")
print(f"Translate API endpoint URL: {translate_api_endpoint}")
As always, replace placeholders like YOUR_ACCOUNT_ID, your-lambda-role, your-bucket-name, lambda-code.zip, and your-aws-region with your actual values. This code provides a framework for integrating different AWS services with Lambda and API Gateway. Adjust and expand this code based on your specific use cases and needs.