0

I have a python class include some methods and I want to run it in Lambda Function and test it. I have a problem with executing in lambda environment .I didn't find any example for using class in Lambda function.I have some error for modifying handler and importing Modules .Is it possible using class in lambda Function? (I also tried to upload my file as a zip file but result is the same.)

3
  • 1
    You can write Python code for AWS Lambda like you would for any other purpose. Do you see any problems in the logs for your lambda function? Commented Aug 25, 2016 at 15:12
  • 1
    If you post your code section that causes the issue, we can more easily help you :-) Commented Aug 25, 2016 at 15:28
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. stackoverflow.com/help/mcve Commented Aug 25, 2016 at 15:35

1 Answer 1

1

I have also the same doubt how to run the below code in aws lambda using function.


import base64
import hashlib

from Crypto import Random
from Crypto.Cipher import AES

class AESCipher(object):

    def __init__(self, key):
        self.bs = 32
        self.key = hashlib.sha256(AESCipher.str_to_bytes(key)).digest()

    @staticmethod
    def str_to_bytes(data):
        u_type = type(b''.decode('utf8'))
        if isinstance(data, u_type):
            return data.encode('utf8')
        return data

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * AESCipher.str_to_bytes(chr(self.bs - len(s) % self.bs))

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s)-1:])]

    def encrypt(self, raw):
        raw = self._pad(AESCipher.str_to_bytes(raw))
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')
    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

       cipher = AESCipher(key='abcd')
       encrypted = cipher.encrypt("Hello World")
       print(encrypted)
       new_cipher = AESCipher(key='abcd')
       decrypted = new_cipher.decrypt('y1sJ7uJITflG81eKHUWd+JkGlOyj3v/iR5HFyiIJv3u9ZpWFCX/XW+A5HS4iPBVb')
       print(decrypted)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.