Questions tagged with AWS Lambda
Content language: English
Sort by most recent
I already train a BERT model in Python 3.9.16 and I save the .pth files in the models directory (my model ia about 417MB) and I also have my Dockerfile and requirements.txt as following:
# Dockerfile
```
FROM public.ecr.aws/lambda/python:3.9-x86_64
ENV TRANSFORMERS_CACHE=/tmp/huggingface_cache/
COPY requirements.txt .
#RUN pip install torch==1.10.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
RUN pip install torch==1.9.0
RUN pip install transformers==4.9.2
RUN pip install numpy==1.21.2
RUN pip install pandas==1.3.2
RUN pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}/dependencies"
COPY app.py ${LAMBDA_TASK_ROOT}
COPY models ${LAMBDA_TASK_ROOT}/dependencies/models
CMD [ "app.handler" ]
```
# requirements.txt
```
torch==1.9.0
transformers==4.9.2
numpy==1.21.2
pandas==1.3.2
```
# app.py
```
import torch
from transformers import BertTokenizer, BertForSequenceClassification, BertConfig
#from keras.preprocessing.sequence import pad_sequences
#from keras_preprocessing.sequence import pad_sequences
#from tensorflow.keras.preprocessing.sequence import pad_sequences
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
import numpy as np
import pandas as pd
from typing import Dict
import json
# Path to the directory containing the pre-trained model files
#model_dir = "./models/"
model_dir= "./dependencies/models/"
dict_path = f"{model_dir}/model_BERT_DAVID_v2.pth"
state_dict = torch.load(dict_path,map_location=torch.device('cpu'))
vocab_path=f"{model_dir}/vocab_BERT_DAVID_v2.pth"
vocab = torch.load(vocab_path,map_location=torch.device('cpu'))
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=4, state_dict=state_dict)
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True, vocab=vocab)
def handler(event):
#payload = json.loads(event)
payload=event # dict with the text
text = payload['text']
df = pd.DataFrame()
df['TEXT']=[text]
sentences = df['TEXT'].values
sentences = ["[CLS] " + sentence + " [SEP]" for sentence in sentences]
tokenized_texts = [tokenizer.tokenize(sent) for sent in sentences]
MAX_LEN = 256
# Use the BERT tokenizer to convert the tokens to their index numbers in the BERT vocabulary
input_ids = [tokenizer.convert_tokens_to_ids(x) for x in tokenized_texts]
# Pad our input tokens
#input_ids = pad_sequences(input_ids, maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")
# Pad our input tokens
input_ids = [torch.tensor(seq)[:MAX_LEN].clone().detach() for seq in input_ids]
input_ids = torch.nn.utils.rnn.pad_sequence(input_ids, batch_first=True, padding_value=0)
input_ids = torch.nn.functional.pad(input_ids, (0, MAX_LEN - input_ids.shape[1]), value=0)[:, :MAX_LEN]
input_ids = input_ids.type(torch.LongTensor)
# Create attention masks
attention_masks = []
# Create a mask of 1s for each token followed by 0s for padding
for seq in input_ids:
seq_mask = [float(i>0) for i in seq];attention_masks.append(seq_mask)
prediction_inputs = input_ids.to('cpu') # cuda
prediction_masks = torch.tensor(attention_masks, device='cpu') # cuda
batch_size = 32
prediction_data = TensorDataset(prediction_inputs, prediction_masks)
prediction_sampler = SequentialSampler(prediction_data)
prediction_dataloader = DataLoader(prediction_data, sampler=prediction_sampler, batch_size=batch_size)
# Prediction
# Put model in evaluation mode
model.eval()
# Tracking variables
predictions = []
# Predict
for batch in prediction_dataloader:
# Add batch to GPU
#batch = tuple(t.to(device) for t in batch)
batch = tuple(t for t in batch)
# Unpack the inputs from our dataloader
b_input_ids, b_input_mask = batch
# Telling the model not to compute or store gradients, saving memory and speeding up prediction
with torch.no_grad():
# Forward pass, calculate logit predictions
logits = model(b_input_ids, token_type_ids=None, attention_mask=b_input_mask)
# Move logits and labels to CPU
logits = logits['logits'].detach().cpu().numpy()
#label_ids = b_labels.to('cpu').numpy()
# Store predictions and true labels
predictions.append(logits)
#true_labels.append(label_ids)
key = {0:'VERY_NEGATIVE', 1:'SOMEWHAT_NEGATIVE', 2:'NEUTRAL',3:'POSITIVE'}
values=np.argmax(predictions[0], axis=1).flatten() # prediccion maxima de likehood
converted_values = [key.get(val) for val in values] # valor del dict al que corresponde al optimo valor de likehood
# Obtain the score for the intensity
exponents = np.exp(predictions) # Operar sobre la softmax para sacar la prob
softmax = exponents / np.sum(exponents)
intensity={'VERY_NEGATIVE':softmax[0][0][0],'SOMEWHAT_NEGATIVE':softmax[0][0][1],'NEUTRAL':softmax[0][0][2],\
'POSITIVE':softmax[0][0][3]}
score=max(intensity.values())
return converted_values[0]
```
Everything seems correct in local when i create the aws lambda function in the 3.9 version I got this error:
```
{
"errorMessage": "invalid load key, 'v'.",
"errorType": "UnpicklingError",
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n",
" File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n",
" File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n",
" File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n",
" File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n",
" File \"/var/task/app.py\", line 25, in <module>\n state_dict = torch.load(dict_path,map_location=torch.device('cpu'))\n",
" File \"/var/lang/lib/python3.9/site-packages/torch/serialization.py\", line 608, in load\n return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)\n",
" File \"/var/lang/lib/python3.9/site-packages/torch/serialization.py\", line 777, in _legacy_load\n magic_number = pickle_module.load(f, **pickle_load_args)\n"
]
}
```
I try multiple things but no solution so far anyone can help me
Hi,
Is there a way to get the AWS Lambda Function URL string (or the bits to construct it) programmatically from the running instance of the Lambda itself?
I tried the below options and neither of them had the necessary URL:
1. checked the input object in `handleRequest(Object input, Context context)`
2. checked the items in `System.getenv()`
Thanks
We deployed a new version of a serverless (python) application yesterday. All the CloudFormation events have it looking like a successful deployment.
The error is that every lambda gets the
` Handler 'handler' missing on module 'routes/file_name'`
We have not made any changes to the structure of our code, nor any changes at all from the AWS console.
The changes we made are to use a newer version of Google Ads library, and also deployed from a new machine that required an updated version of `serverless` and `node` packages (plus whatever changed in their dependencies.)
```
$node --version
v16.19.1
$serverless --version
Running "serverless" from node_modules
1.30.1
```
I tried
- Rolling back one of the lambdas by specifying an older version int he API Gateway function that acts as a pass-through to the lambda.
- Deploying a previous version of our code, which I believe is exactly what was already being used by the lambda
- Creating an alias of an old version of the lambda, but couldn't figure out what to do with it.
I also double-checked the CloudWatch logs and verified that things were working correctly before the new deplioyment.
And finally, we deployed another app with a single lamda that gets its input from an SQS queue, with the same (broken) result.
This is causing a production outage of functionality that is important to our customers.
Hi All. I have had multiple emails recently from AWS with subject line “[ACTION REQUIRED] - Update your TLS connections to 1.2 to maintain AWS endpoint connectivity [AWS Account: 090759423501]”. The key part of the email seems to be
.....................
Please see the following for further details on the TLS 1.0 or TLS 1.1 connections detected from your account between February 25, 2023 and March 13, 2023 (the UserAgent may be truncated due to a limit in the number of characters that can be displayed):
Region | Endpoint | API Event Name | TLS Version | Connection Count | UserAgent
eu-west-1 | dynamodb.eu-west-1.amazonaws.com | DescribeTable | TLSv1 | 324 | aws-sdk-dotnet-45/3.3.1.0 aws-sdk-dotnet-core/3.3.5.0 .NET_Runtime/4.0 .NET_Framework/4.0 OS/Microsoft_Windows_NT_10.0.14393.0 ClientSync Docu
............................
However, my reading of that is that it is a system call from a .net runtime and I’m not really sure what I can do about this.
Your assistance would be appreciated. I typically use AWS resources in two ways:
a) scheduled or triggered Lambda calls built directly in the AWS interface or
b) calls from c# .net programs coded in Visual Studio
I followed some links to literature that the AWS account manager gave me which suggested
1) Use the Cloud Trail section of CloudWatch to find log entries where TLS 1.0 or 1.1 was used - I tried this but could find no matching records when I ran the query
2) Check the general account health dashboard - I did this but no problems are reported there.
Can anyone suggest a course of action here?
Thanks
Richard Abbott
My lambda takes a long time to do the first operation with an aws client.
For example I am performing a query on the index through the dynamoDB client and the first execution takes 2 seconds, while in subsequent executions on the same lambda environment the query is executed in 100 milliseconds.
The DynamoDB client is inizialized outside of the Lambda handler method.
Why my first execution takes so long?
**Error ----------------------------------------------------------------------------------------------: **
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<StringToSign>PUT
**Code in Python----------------------------------------------------------------------------------------------: **
import botocore
import boto3
import datetime
from botocore.exceptions import ClientError
import json
s3_file="test6.jpg"
def lambda_handler(event, context):
# TODO implement
print("Step 1")
try:
s3 = boto3.client('s3')
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': 's3_Bucket',
'Key': s3_file
},
ExpiresIn=36000000
)
result = {'status': 'success', 'data': {'url': url, 'key':s3_file}}
response = {
'statusCode': 200,
'body': json.dumps(result),
'headers': {
'Content-Type': 'application/json'
}
}
except ClientError as e:
print(f'Error generating presigned URL: {e}')
return None
return response
Hi all,
I'm building a chatbot using: `Twilio -> Lexv2 -> Lambda`
I would like to capture the phone number of the user that is currently texting the bot in order to log information about the interaction along with a couple of other things.
Right now the only request attribute that is coming across in the event is:
```
"x-amz-lex:channels:platform": "Twilio"
```
Is there anything I need to enable in order to get the phone number that started the interaction passed through in the request attributes?
Hi there,
I was reading the official documentation on https://github.com/aws/aws-xray-sdk-go but not quite sure how can I add the Cognito client that is created on my lambda function to the service map and x-ray traces.
```
import (
"context"
cognito "github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-xray-sdk-go/xray"
)
func GetClient() {
...
cfg, errCfg := config.LoadDefaultConfig(context.TODO())
client := cognito.NewFromConfig(cfg)
// the next line is not valid, because the function receives a *client.Client as a parameter
// and my current client si of type *cognito.Client
// the error is: cannot use client (variable of type *cognitoidentityprovider.Client) as *"github.com/aws/aws-sdk-go/aws/client".Client value in argument to xray.AWS
xray.AWS(client)
...
}
```
I appreciate your advice.
When I run the lambda (.net 6) code locally pointing to the SES host, it works. But, when I call the lambda function, it returns 503 error and don´t send the email.
Considerations:
- The lambda function is under a vpc´s public subnet
- I'm using the .NET MailKit package to send the emails:
public void Send(string to, string subject, string html, string from = null)
{
// create message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(from ?? _appSettings.EmailFrom));
email.To.Add(MailboxAddress.Parse(to));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = html };
// send email
using var smtp = new SmtpClient();
smtp.Connect(_appSettings.SmtpHost, _appSettings.SmtpPort, SecureSocketOptions.Auto);
smtp.Authenticate(_appSettings.SmtpUser, _appSettings.SmtpPass);
smtp.Send(email);
smtp.Disconnect(true);
}
I´m using the sandbox setting and both (sender and receiver) are verified. I have also created an policy like bellow and attached it to the lambda role:
}
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}
Could anyone help me with this issue please?
Hello, I was trying to build FTPS server using Transfer family, But I couldn't able to successfully build one. Could some one explain in details how to build one in detail.
I tried browsing online for guidance all I could find is for building SFTP server. I need help in building "custom identity provider" using rest API and lambda function. I couldn't find the code for the lambda function.
I am seeking guidance on how to resolve the problem with Elasticache Memcached that we are currently encountering. Our setup consists of a 2-node cluster, and our application is based on Lambda. To ensure read availability, we write requests to both nodes. However, we are facing random connection timeouts that last for 10 to 30 seconds, and we receive "get_misses" errors. Upon checking the metrics, the CPU usage is low (around 5%) and there is sufficient available memory. Can someone offer suggestions on how to troubleshoot this issue?
I need to create a lambda function without putting any source, so that terraform script will just create a lambda. This will be in infra repo you can say that where I am just provisioning the services.
Then in another repo which will be used by developers where we will have a application code which needs to be deployed in that lambda.
So when I am creating the lambda using terraform in Infra repo, then source_path seems mandatory... please suggest how I can achieve this.