question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import boto3==1.1.4 on AWS Lambda

See original GitHub issue

I am trying to use boto3==1.1.4 on AWS Lambda.

I did not find a way to import this version, even though I have in included in the zip archive.

Running the code locally it reports version 1.1.4

On AWS Lambda it reports only version 1.1.3.

Trying to request specific version by means of pkg_resources.require(“boto3==1.1.4”) does not improve the situation.

This is my code:

from lxml import etree
from py.path import local
import json
import gzip
import pkg_resources
pkg_resources.require('boto3==1.1.4')
import boto3

print(boto3.__version__)
assert boto3.__version__ == "1.1.4"

s3 = boto3.resource("s3")


def fetch(bucket_name, key_name, version_id, fname):
    keyver = s3.ObjectVersion(bucket_name, key_name, version_id).Object()

    assert "download_file" in dir(keyver)
    keyver.download_file(fname.strpath)
    return fname.strpath


def transform(fname):
    doc = etree.parse(fname.strpath)
    ids = doc.xpath("//ElaboratedRecord/@id")
    data = json.dumps(ids)
    return data


def publish(data, bucket_name, key_name, tmpdir):
    fname = tmpdir / "es.json.gz"

    try:
        with gzip.open(fname.strpath, "wb") as gf:
            gf.write(data)
        extra_args = {"ContentType": "application/json",
                      "ContentEncoding": "gzip"}
        s3.Object(bucket_name, key_name).upload_file(fname.strpath,
                                                     ExtraArgs=extra_args)
    finally:
        fname.remove()


def main(event, context):
    assert len(event["Records"]) == 1
    s3rec = event["Records"][0]["s3"]
    bucket_name = s3rec["bucket"]["name"]
    key_name = s3rec["object"]["key"]
    version_id = s3rec["object"]["versionId"]

    tmpdir = local.mkdtemp()
    infname = tmpdir / "es.xml.gz"
    try:
        fetch(bucket_name, key_name, version_id, infname)
        data = transform(infname)
        pubbucket = "sandbox.dp.ce-traffic.com"
        pubkey = "region/cz/EventService.json"
        publish(data, pubbucket, pubkey, tmpdir)
    finally:
        tmpdir.remove(rec=1, ignore_errors=True)

When I run this code, log reports:

START RequestId: 1fbc70e5-76a6-11e5-bb30-f17c9f6ee1a6 Version: $LATEST
module initialization error: (boto3 1.1.3 (/var/runtime), Requirement.parse('boto3==1.1.4'))

END RequestId: 1fbc70e5-76a6-11e5-bb30-f17c9f6ee1a6

This is probably more related to AWS Lambda environment itself, but I have no other place to report this problem.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
vlcinskycommented, Oct 23, 2015

Thanks @mtdowling

I have found description of my problem in the forum: https://forums.aws.amazon.com/thread.jspa?threadID=217554&tstart=0

  • it is considered a bug, which shall be resolved and resolution reported to given thread
  • there is simple workaround mentioned there.

As this is not related to boto3 but to AWS Lambda, I consider this issue to be closed (in boto3 tracker).

Btw, the workaround looks as follows:

import os
import os.path
import sys

root = os.environ["LAMBDA_TASK_ROOT"]
sys.path.insert(0, root)
import boto3 #should grab boto3 from included zip first 
6reactions
secureoptionscommented, Jul 18, 2019

If you don’t want to package a more recent boto3 version with you function, you can download boto3 with each invocation of the Lambda. Remember that /tmp/ is the directory that Lambda will allow you to download to, so you can use this to temporarily download boto3:

import sys
from pip._internal import main

main(['install', 'boto3', '--target', '/tmp/'])
sys.path.insert(0,'/tmp/')

import boto3
from botocore.exceptions import ClientError

def handler(event, context):
    print(boto3.__version__)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda Functions with Newer Version of boto3 than Available ...
This blog post will explore using boto3 1.4.8 and botocore 1.8.0 despite (at the time of this writing) the Lambda execution environment ......
Read more >
Lambda examples using SDK for Python (Boto3)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Python (Boto3) with...
Read more >
class Lambda. Client - Boto3 Docs 1.26.34 documentation
import boto3 client = boto3.client('lambda'). These are the available methods: add_layer_version_permission(); add_permission(); can_paginate(); close() ...
Read more >
Numpy Import Error and Aws lambda layer folder structure ...
What I recommend you to do instead, is to locally install the libraries you need in a ... Successfully installed numpy-1.23.2 pandas-1.4.4 ......
Read more >
Homepage - AWS Lambda Powertools for TypeScript
A suite of utilities for AWS Lambda functions running on the Node.js runtime, to ease adopting best ... npm: npm install @aws-lambda-powertools/tracer ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found