trigger lambda from SQS event on different VPC - amazon-web-services

I have my SQS URL which is configured in one VPC and want to trigger Lambda function from another VPC based on any new message that comes in SQS. How can i achieve it for real time analysis.

Amazon SQS is not associated with Amazon VPC. It exists "outside" of VPCs.
Therefore, you can simply configure the Lambda function to use the SQS queue as a trigger.

Related

python lambda code for aws ec2 gets stopped

could any one please help me the lambda code , whenever AWS Ec2 instances get stopped, we need to get the email notifications with sns. In the email we need instance name. I could able to get instance id but not the instance name.
AWS CloudTrail allows you to identify and track EC2 instance lifecycle API calls (launch, start, stop, terminate). See How do I use AWS CloudTrail to track API calls to my Amazon EC2 instances?
And you can trigger a Lambda function to run arbitrary code when CloudTrail logs certain events. See Triggering a Lambda function with AWS CloudTrail events.
You can also create an Amazon CloudWatch alarm that monitors an Amazon EC2 instance and triggers a Lambda via CloudWatch Events.
You can create a rule in Amazon CloudWatch Events that:
Triggers when an instance enters the Stopped state
Sends a message to an Amazon SNS Topic
Like this:
If you want to modify the message that is being sent, then configure the Rule to trigger an AWS Lambda function instead. Your function should:
Extract the instance information (eg InstanceId) from the event parameter
Call describe-instances to obtain the Name of the instance (presumably the Tag with a Key of Name)
Publish a message to the Amazon SNS Topic

Create a alarm to notify when ip addresses are running low

Is there any way to create a alarm in AWS for ip-addresses are about to running out?
Thanks!
There is no standard Amazon CloudWatch alarm for monitoring available IP addresses in a subnet. However, you could:
Create an AWS Lambda function that scans the subnet(s) and sends a custom metric to CloudWatch
Create an Amazon CloudWatch Events rule that triggers the Lambda function at a desired interval
Create an Alarm on the custom metric to notify you when the metric drops below a desired level
The benefit of this approach is that you will retain a history, within the custom metric, of the number of available IP addresses. However, it would involve a separate custom metric and alarm for each subnet.
Alternatively, you could configure the Lambda function to send a message to an Amazon SNS topic whenever there is a low quantity of IP addresses in a subnet. Then, subscribe to the SNS topic to receive notification. While there is no history available, it requires less configuration because it doesn't use CloudWatch.

AWS - is there a way to receive events asynchronously without the lambda

I have a general AWS question. I have started using AWS sdk, but looks like if I want to receive events asynchronously from AWS(ex: cloudwatch events), lambda functions is the only way. I want to write a simple application that registers a callback to AWS for events, but i couldn't find a way to do that till now, since i don't want to use lambda, i have been polling from my application. Please, let me know if polling is the only option or if there is a better way to resolve it without polling.
From the documentation:
You can configure the following AWS services as targets for CloudWatch Events:
Amazon EC2 instances
AWS Lambda functions
Streams in Amazon Kinesis Streams
Delivery streams in Amazon Kinesis Firehose
Amazon ECS tasks
SSM Run Command
SSM Automation
Step Functions state machines
Pipelines in AWS CodePipeline
Amazon Inspector assessment templates
Amazon SNS topics
Amazon SQS queues
Built-in targets
The default event bus of another AWS account
That's a lot more than just Lambda, so I'm not sure why you state in your question that Lambda is the only option. The options of Amazon EC2 instances and Amazon SNS topics both provide a method for Amazon to "push" the events to your services, instead of requiring your services to poll.
With cloudwatch events, you can set rules and trigger a number of different targets, including SQS queues which you can poll from your EC2 Instances.
Lambda is certainly a popular endpoint, but based on the docs, there are other targets you can send the events to
Already above answers might also be helpful, but one of the possible options to address your problem could be one of this as well.
You can make use of AWS SNS service to subscribe for the events on AWS resources. And the SNS can publish the events to your application end point. Which is nothing but pub/sub model.
Refer this link http://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html
The end-point could be your http or https based application.

Cloudwatch trigger to Aws lambda

I am using cloudwatch to trigger AWS Lambda. Now to create this trigger first of all I have to create rule then I need to add target and then I need to add permission policy to that lambda function. Now the problem is there is a policy length limit(20k) for each lambda function and each cloudwatch trigger increase policy length. So if there are around 60 cloudwatch rules my lambda can still handle all of those events as size is still less than 20k, but after that if rules increase I get http://docs.aws.amazon.com/lambda/latest/dg/API_AddPermission.html "policy length exceed exception error".
I know I can delete the policy by using removePermission method but that is not efficient because if there are around 100 rules present in cloudwatch which are triggering my lambda function then my lambda function can't handle that much amount of policy length.
This is how I solved it.
Cloudwatch Alerts --> SNS --> Lambda
No subscription, no permissions configuration needed. You can create multiple different SNS notifications if you want to isolate different team based on your alerts.
Steps:
Create SNS with a topic
Send Cloudwatch Alerts to SNS
Subscribe Lambda to that SNS Topic
Receive Alerts to Lambda!
Hope it helps.

Can a CloudWatch Event in one region trigger a Lambda in another region of AWS?

Can an AWS CloudWatch event in region us-east-1 trigger a lambda in us-west-2? Or do I have to deploy my lambda in both regions?
An alternative is to have CloudWatch trigger SNS. As per the attached screenshot, SNS topic subscription will allow full ARN declaration, and the dropdown even showed a us-east-1 lambda when I was in US West (Oregon) region.
SNS Screenshot
From the dashboard, it is not possible to trigger cross region Lambda. When you create a CloudWatch event rule, select a Target to invoke, under Lambda function only the lambdas in the current region are shown.
This could be achieved by creating a the Event Rule in the source Region as well as SNS Topic in the same Region as a Target.
Then subscribe a Lambda in the target region to this SNS Topic.
You may relay an event to a Eventbus in the desired region. There you can set an event rule to trigger the desired lambda.