AWS Python boto3 lambda - getting the kinesis stream name - python-2.7

I am a lambda in aws python boto3. I configured this lambda (a trigger) to read from a specific kinesis stream. It has to read the stream name from the event object. I don't know how to get the stream name. I don't want to hard code the Kinesis stream name in the code. I want to read it from event.
import boto3
import json
from pprint import pprint
s3 = boto3.client('s3')
kinesis = boto3.client('kinesis')
def lambda_handler(event, context):
if event:
for i in event["Records"]:
print(i)

You can see an example Kinesis record event at Using AWS Lambda with Amazon Kinesis.
Each record contains an eventSourceARN. To print the ARN of each event record:
def lambda_handler(event, context):
for record in event["Records"]:
print(record["eventSourceARN"])
You can infer the Kinesis stream name, if needed, from the ARN.

Related

Alternative for context.succeed(event)

Iam trying to create a migration trigger using python to change my user pool.In this trigger, what will be the alternative for context.succeed(event) that needs to be sent back to cognito after successful verification of user.
According to the docs, you can return the event
def lambda_handler(event, context):
# Your code here
# Return to Amazon Cognito
return event
Docs

Subscribe an Amazon SNS topic using AWS Lambda fn that is sns.subscribe()?

I want to send an SNS subscription request to a Topic for email subscription,
As I'm new to Lambda, Can you please let me know what libraries needs to be imported and what functions needs to be triggered.
Thanks in advance.
Import Boto3 Package
Create a Reference to SNS Boto
Create a SNS Topic
Create a Email Susbcriber to that SNS Topic.
(make sure you follow the indentation in python. It a running code)
import boto3
snsClient = boto3.client('sns')
def lambda_handler(event, context):
topicName = "myFirstTopic"
emailId= "yourname#company.com"
#creating Topic and if it already created with the specified name, that topic's ARN is returned without creating a new topic..
snsArn = createSNSTopic(topicName)
#crearting a subscriber for the specified SNS Toic
snsEndpointSusbcribe(snsArn,emailId)
#Function to create SNS Topic
def createSNSTopic(topicName):
try:
response = snsClient.create_topic(
Name=topicName
)
print(response['TopicArn'])
return response['TopicArn']
except Exception as ex:
print(ex)
#Function to create SNS Subscriber for that Topic
def snsEndpointSusbcribe(snsArn,emailId):
response = snsClient.subscribe(
#
TopicArn=snsArn,
Protocol='email',
Endpoint=emailId,
)

Getting message: “Internal server error” while invoking AWS Lambda via API Gateway. The function is working but the response is this error

I am accessing the following function created in lambda:
import json
import boto3
from get_json_s3 import get_json
def lambda_handler(event, context):
print(event)
jobId = event['queryStringParameters']['jobID']
try:
print(jobId)
response = get_json(None,f'{jobId}.json')
print(response)
except:
return {
'statusCode': 200,
'body': json.dumps('jobID not found')
}
print("success")
return {
'statusCode': 200,
'body': json.dumps(response)
}
get_json is defined as follows:
import json
import boto3
s3 = boto3.client('s3')
def get_json(filegroup, filename):
bucket = 'bucket name'
if filegroup!=None:
key = f'{filegroup}/{filename}'
else:
key = f'{filename}'
response = s3.get_object(Bucket = bucket, Key = key)
content = response['Body']
jsonObject = json.loads(content.read())
return jsonObject
I have created a API gateway with lambda as a proxy. I have added the invoke access and apigateway access to lambda function but I keep getting 502: Internal Server Error.
The lambda is doing the function it is supposed to do correctly as I can see from Cloud watch logs. It is being triggered correctly via APIgateway too. Only the response part is not working
Here is the common issues which might be able to help you diagnose the issue.
It doesn't have a right permission to allow API Gateway invoke your Lambda function.
It is set as AWS Service Proxy to your Lambda function, the response from your Lambda function doesn't return the response in the proper format.
I recommend you to enable logging feature on API Gateway side or you can use the test invoke feature on API Gateway console using this doc.

Send username to AWS Lambda function triggered when user is registered through AWS Cognito

I am trying to write a Lambda function that makes a folder in an s3 bucket when a newly confirmed Cognito user. This will allow me to keep that user's access limited to their folder. I have created a Lambda function that can list the current users registered in the user pool. I know Cognito has a "confirmation event" and "post authentication" event trigger, and I have selected my function to run on that trigger.
But I do not know how to make the folder when the user authenticates or confirmed from that event. Screenshot below is of my Lambda code.
Here is my code for post authentication trigger, but it does not work:
from __future__ import print_function
def lambda_handler(event, context):
# Send post authentication data to Cloudwatch logs
print ("Authentication successful")
print ("Trigger function =", event['triggerSource'])
print ("User pool = us-east-1_EVPcl4p64", event['userPoolId'])
print ("App client ID = ", event['callerContext']['clientId'])
print ("User ID = ", event['userName'])
# Return to Amazon Cognito
return event
Here is the code for list user. It works, but now how to fetch only user name and on that basis create a folder in an s3 bucket?
import json
import boto3
import re
def lambda_handler(event, context):
# TODO implement
client = boto3.client('cognito-idp')
response = client.list_users(
UserPoolId='us-east-1_EVPcl4p64',
AttributesToGet=['email']
)
x = response
print json.dumps(x)
print(y["email"])
pattern = '^#gmail.com$'
test_string = response
result = re.match(pattern, test_string)
print(response)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Use PostConfirmation trigger instead. Post authentication trigger will fire every time when a user signs in.
Here is how you get the email address:
email = event['request']['userAttributes']['email']
Here is how you create an S3 "folder" for that user using email as the folder name:
s3 = boto3.client('s3')
bucket_name = 'example-bucket-name'
directory_path = f"users/{email}/"
s3.put_object(Bucket=bucket_name, Key=directory_path)

How to stop/start services of ambari cluster using AWS Lambda and AWS API Gateway

I wan't to pass the web services to AWS Lambda so that using those web services I can stop/start the services of ambari cluster.
Thanks in advance.
AWS Lambda can be easily integrated with almost all webservices including EC2 through programmatic api calls using boto3
You just need to create a boto3 client of the aws service in lambda function and you can use them the way you want (for e.g start/stop) .
AWS Lambda also provides you to schedule its invocation.
As you have mentioned in the comment , you need to stop them using an api.
Here is the basic code snippet that works -
# you need to create a zip that includes requests lib and upload it in lambda function
import requests
# name of lambda_function that needs to invoke
def lambda_handler(event, context):
url = ''
json_body = {}
try:
api_response = requests.put(url=url, json=json_body)
except Exception as err:
print(err)
If you have these servers running in aws then you can create a boto3 client and use
import boto3
from boto3.session import Session
aws_access_key = 'xxxxxxxxxxxxxxxxxxxx'
aws_secret_key = 'yyyyyyyyyyyyyyyyyyyyy'
region = 'xx-yyyyyy'
def lambda_handler(event, context):
try:
sess = Session(aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key)
ec2_conn = sess.client(service_name='ec2', region_name=region)
response = client.start_instances(
InstanceIds=[
'i-xxxxxx','i-yyyyyy','i-zzzzzz',
])
except ClientError as e:
print(e.response['Error']['Message'])
P.S: this is a basic snippet and can vary according to your use case