Unable to run Bash Script using AWS Custom Lambda Runtime

30 views Asked by At

I have the following bash shell script which I want to schedule every day at 5:00 PM using lambda custom run time.

#!/usr/bin/bash 
declare -a end_points
end_points=$(aws ec2 describe-vpc-endpoints --filters "Name=vpc-endpoint-type,Values=Interface" --query 'VpcEndpoints[*].VpcEndpointId' --output text) 
for ((i=0; i < ${#end_points[@]}; i++ ));
do 
   echo "${end_points[$i]}"; 
done

Here is what I did using the AWS console -

  1. Selected Amazon Linux 2 as the custom runtime
  2. Added the layer -- arn:aws:lambda::744348701589:layer:bash:8
  3. Here is my function.sh ( Just to test before I could run my above script) --
function handler ()  
{ EVENT_DATA=$1  
  DATA=$(aws s3 ls)  
  RESPONSE="{\"statusCode\": 200, \"body\": \"$DATA\"}"  
  echo $RESPONSE 

I am getting the following error -- /var/task/function.sh: line 10: /opt/bin/aws: cannot execute: required file not found

1

There are 1 answers

1
Cloudlady On

The error message you're seeing indicates that the AWS CLI is not found in the expected location (/opt/bin/aws). This is because the AWS CLI is not included in the Bash layer you're using (arn:aws:lambda::744348701589:layer:bash:8).

To use the AWS CLI in your Lambda function, you have two options:

  1. Create a custom AWS Lambda layer that includes the AWS CLI: You can create a zip file that includes the AWS CLI and any other dependencies your function needs, and then create a new Lambda layer using that zip file. Once the layer is created, you can add it to your Lambda function.

  2. Use the AWS SDK instead of the AWS CLI: The AWS SDKs (Software Development Kits) are available in many programming languages and include the same functionality as the AWS CLI. You can rewrite your function to use the AWS SDK instead of the AWS CLI. For example, if you're comfortable with Python, you can use the Boto3 library (the AWS SDK for Python) to interact with AWS services.

More generally, it sounds like a linux machine with a cron job would make a better fit. Using AWS Lambda for this task could be overkill, especially if the script takes a long time to run or if it doesn't fit well with the event-driven model of Lambda. AWS Lambda is best suited for short, event-driven tasks, while cron jobs are better for longer, time-based tasks. If you are not interested in assigning a VM for that, you can containerize your application isong a Docker container with a cron job inside it.