2016-02-16 20:15:34 +00:00
|
|
|
import base64
|
2019-08-21 01:54:57 +00:00
|
|
|
import time
|
2017-09-27 23:04:58 +00:00
|
|
|
from collections import defaultdict
|
2017-11-26 21:28:28 +00:00
|
|
|
import copy
|
2016-02-12 19:39:20 +00:00
|
|
|
import datetime
|
2020-05-12 12:34:10 +00:00
|
|
|
from gzip import GzipFile
|
2021-10-13 21:35:38 +00:00
|
|
|
from sys import platform
|
2020-05-12 12:34:10 +00:00
|
|
|
|
2018-06-01 02:39:22 +00:00
|
|
|
import docker
|
2017-09-27 23:04:58 +00:00
|
|
|
import docker.errors
|
2016-02-16 20:15:34 +00:00
|
|
|
import hashlib
|
2016-10-06 09:52:23 +00:00
|
|
|
import io
|
2017-09-27 23:04:58 +00:00
|
|
|
import logging
|
2017-05-24 12:54:00 +00:00
|
|
|
import os
|
2016-06-22 19:24:46 +00:00
|
|
|
import json
|
2017-09-27 23:04:58 +00:00
|
|
|
import re
|
2016-10-06 09:52:23 +00:00
|
|
|
import zipfile
|
2017-09-27 23:04:58 +00:00
|
|
|
import uuid
|
|
|
|
import tarfile
|
|
|
|
import calendar
|
|
|
|
import threading
|
2017-11-26 21:28:28 +00:00
|
|
|
import weakref
|
2020-11-08 15:16:53 +00:00
|
|
|
import requests.exceptions
|
2016-10-06 14:14:47 +00:00
|
|
|
|
2020-01-23 18:46:24 +00:00
|
|
|
from moto.awslambda.policy import Policy
|
2022-03-17 12:32:31 +00:00
|
|
|
from moto.core import BaseBackend, BaseModel, CloudFormationModel
|
2017-11-26 21:28:28 +00:00
|
|
|
from moto.core.exceptions import RESTError
|
2019-11-07 17:11:13 +00:00
|
|
|
from moto.iam.models import iam_backend
|
|
|
|
from moto.iam.exceptions import IAMNotFoundException
|
2021-12-24 21:02:45 +00:00
|
|
|
from moto.core.utils import unix_time_millis, BackendDict
|
2016-02-16 21:43:33 +00:00
|
|
|
from moto.s3.models import s3_backend
|
2017-09-27 23:04:58 +00:00
|
|
|
from moto.logs.models import logs_backends
|
2017-05-24 12:54:00 +00:00
|
|
|
from moto.s3.exceptions import MissingBucket, MissingKey
|
2017-09-27 23:04:58 +00:00
|
|
|
from moto import settings
|
2019-11-17 10:59:20 +00:00
|
|
|
from .exceptions import (
|
|
|
|
CrossAccountNotAllowed,
|
|
|
|
InvalidRoleFormat,
|
|
|
|
InvalidParameterValueException,
|
2022-03-17 12:32:31 +00:00
|
|
|
UnknownLayerException,
|
2022-03-15 15:07:01 +00:00
|
|
|
UnknownFunctionException,
|
2022-03-17 12:32:31 +00:00
|
|
|
UnknownAliasException,
|
2019-11-17 10:59:20 +00:00
|
|
|
)
|
2021-01-17 15:28:49 +00:00
|
|
|
from .utils import (
|
|
|
|
make_function_arn,
|
|
|
|
make_function_ver_arn,
|
|
|
|
make_layer_arn,
|
|
|
|
make_layer_ver_arn,
|
|
|
|
split_layer_arn,
|
|
|
|
)
|
2019-08-21 01:54:57 +00:00
|
|
|
from moto.sqs import sqs_backends
|
2022-03-09 17:57:25 +00:00
|
|
|
from moto.dynamodb import dynamodb_backends
|
2019-10-07 10:11:22 +00:00
|
|
|
from moto.dynamodbstreams import dynamodbstreams_backends
|
2022-05-08 22:25:40 +00:00
|
|
|
from moto.core import get_account_id
|
2021-02-18 08:58:20 +00:00
|
|
|
from moto.utilities.docker_utilities import DockerModel, parse_image_ref
|
2022-03-17 12:32:31 +00:00
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from uuid import uuid4
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
docker_3 = docker.__version__[0] >= "3"
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2018-06-01 03:05:50 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
def zip2tar(zip_bytes):
|
|
|
|
with TemporaryDirectory() as td:
|
2019-10-31 15:44:26 +00:00
|
|
|
tarname = os.path.join(td, "data.tar")
|
|
|
|
timeshift = int(
|
|
|
|
(datetime.datetime.now() - datetime.datetime.utcnow()).total_seconds()
|
|
|
|
)
|
|
|
|
with zipfile.ZipFile(io.BytesIO(zip_bytes), "r") as zipf, tarfile.TarFile(
|
|
|
|
tarname, "w"
|
|
|
|
) as tarf:
|
2017-09-27 23:04:58 +00:00
|
|
|
for zipinfo in zipf.infolist():
|
2019-10-31 15:44:26 +00:00
|
|
|
if zipinfo.filename[-1] == "/": # is_dir() is py3.6+
|
2017-09-27 23:04:58 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
tarinfo = tarfile.TarInfo(name=zipinfo.filename)
|
|
|
|
tarinfo.size = zipinfo.file_size
|
|
|
|
tarinfo.mtime = calendar.timegm(zipinfo.date_time) - timeshift
|
|
|
|
infile = zipf.open(zipinfo.filename)
|
|
|
|
tarf.addfile(tarinfo, infile)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
with open(tarname, "rb") as f:
|
2017-09-27 23:04:58 +00:00
|
|
|
tar_data = f.read()
|
|
|
|
return tar_data
|
|
|
|
|
|
|
|
|
|
|
|
class _VolumeRefCount:
|
|
|
|
__slots__ = "refcount", "volume"
|
|
|
|
|
|
|
|
def __init__(self, refcount, volume):
|
|
|
|
self.refcount = refcount
|
|
|
|
self.volume = volume
|
2016-02-12 19:39:20 +00:00
|
|
|
|
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
class _DockerDataVolumeContext:
|
2019-10-31 15:44:26 +00:00
|
|
|
_data_vol_map = defaultdict(
|
|
|
|
lambda: _VolumeRefCount(0, None)
|
|
|
|
) # {sha256: _VolumeRefCount}
|
2017-09-27 23:04:58 +00:00
|
|
|
_lock = threading.Lock()
|
|
|
|
|
|
|
|
def __init__(self, lambda_func):
|
|
|
|
self._lambda_func = lambda_func
|
|
|
|
self._vol_ref = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._vol_ref.volume.name
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
# See if volume is already known
|
|
|
|
with self.__class__._lock:
|
2022-03-17 12:32:31 +00:00
|
|
|
self._vol_ref = self.__class__._data_vol_map[self._lambda_func.code_digest]
|
2017-09-27 23:04:58 +00:00
|
|
|
self._vol_ref.refcount += 1
|
|
|
|
if self._vol_ref.refcount > 1:
|
|
|
|
return self
|
|
|
|
|
|
|
|
# See if the volume already exists
|
|
|
|
for vol in self._lambda_func.docker_client.volumes.list():
|
2022-03-17 12:32:31 +00:00
|
|
|
if vol.name == self._lambda_func.code_digest:
|
2017-09-27 23:04:58 +00:00
|
|
|
self._vol_ref.volume = vol
|
|
|
|
return self
|
|
|
|
|
|
|
|
# It doesn't exist so we need to create it
|
2019-10-31 15:44:26 +00:00
|
|
|
self._vol_ref.volume = self._lambda_func.docker_client.volumes.create(
|
2022-03-17 12:32:31 +00:00
|
|
|
self._lambda_func.code_digest
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2018-06-01 02:39:22 +00:00
|
|
|
if docker_3:
|
2019-10-31 15:44:26 +00:00
|
|
|
volumes = {self.name: {"bind": "/tmp/data", "mode": "rw"}}
|
2018-06-01 02:39:22 +00:00
|
|
|
else:
|
2019-10-31 15:44:26 +00:00
|
|
|
volumes = {self.name: "/tmp/data"}
|
2022-03-17 12:32:31 +00:00
|
|
|
|
2021-02-18 08:58:20 +00:00
|
|
|
self._lambda_func.docker_client.images.pull(
|
|
|
|
":".join(parse_image_ref("alpine"))
|
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
container = self._lambda_func.docker_client.containers.run(
|
|
|
|
"alpine", "sleep 100", volumes=volumes, detach=True
|
|
|
|
)
|
2017-09-27 23:04:58 +00:00
|
|
|
try:
|
|
|
|
tar_bytes = zip2tar(self._lambda_func.code_bytes)
|
2019-10-31 15:44:26 +00:00
|
|
|
container.put_archive("/tmp/data", tar_bytes)
|
2017-09-27 23:04:58 +00:00
|
|
|
finally:
|
|
|
|
container.remove(force=True)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
with self.__class__._lock:
|
|
|
|
self._vol_ref.refcount -= 1
|
|
|
|
if self._vol_ref.refcount == 0:
|
|
|
|
try:
|
|
|
|
self._vol_ref.volume.remove()
|
|
|
|
except docker.errors.APIError as e:
|
|
|
|
if e.status_code != 409:
|
|
|
|
raise
|
|
|
|
|
|
|
|
raise # multiple processes trying to use same volume?
|
|
|
|
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def _zipfile_content(zipfile):
|
|
|
|
# more hackery to handle unicode/bytes/str in python3 and python2 -
|
|
|
|
# argh!
|
|
|
|
try:
|
|
|
|
to_unzip_code = base64.b64decode(bytes(zipfile, "utf-8"))
|
|
|
|
except Exception:
|
|
|
|
to_unzip_code = base64.b64decode(zipfile)
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
sha_code = hashlib.sha256(to_unzip_code)
|
|
|
|
base64ed_sha = base64.b64encode(sha_code.digest()).decode("utf-8")
|
|
|
|
sha_hex_digest = sha_code.hexdigest()
|
|
|
|
return to_unzip_code, len(to_unzip_code), base64ed_sha, sha_hex_digest
|
|
|
|
|
|
|
|
|
|
|
|
def _s3_content(key):
|
|
|
|
sha_code = hashlib.sha256(key.value)
|
|
|
|
base64ed_sha = base64.b64encode(sha_code.digest()).decode("utf-8")
|
|
|
|
sha_hex_digest = sha_code.hexdigest()
|
|
|
|
return key.value, key.size, base64ed_sha, sha_hex_digest
|
2021-01-17 15:28:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _validate_s3_bucket_and_key(data):
|
|
|
|
key = None
|
|
|
|
try:
|
|
|
|
# FIXME: does not validate bucket region
|
|
|
|
key = s3_backend.get_object(data["S3Bucket"], data["S3Key"])
|
|
|
|
except MissingBucket:
|
|
|
|
if do_validate_s3():
|
|
|
|
raise InvalidParameterValueException(
|
|
|
|
"Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist"
|
|
|
|
)
|
|
|
|
except MissingKey:
|
|
|
|
if do_validate_s3():
|
|
|
|
raise ValueError(
|
|
|
|
"InvalidParameterValueException",
|
|
|
|
"Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.",
|
|
|
|
)
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
2021-02-15 10:31:33 +00:00
|
|
|
class Permission(CloudFormationModel):
|
|
|
|
def __init__(self, region):
|
|
|
|
self.region = region
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_name_type():
|
|
|
|
return "Permission"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_type():
|
|
|
|
return "AWS::Lambda::Permission"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_from_cloudformation_json(
|
2021-11-03 21:00:42 +00:00
|
|
|
cls, resource_name, cloudformation_json, region_name, **kwargs
|
2021-02-15 10:31:33 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
|
|
|
backend = lambda_backends[region_name]
|
|
|
|
fn = backend.get_function(properties["FunctionName"])
|
|
|
|
fn.policy.add_statement(raw=json.dumps(properties))
|
|
|
|
return Permission(region=region_name)
|
|
|
|
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
class LayerVersion(CloudFormationModel):
|
|
|
|
def __init__(self, spec, region):
|
|
|
|
# required
|
|
|
|
self.region = region
|
|
|
|
self.name = spec["LayerName"]
|
|
|
|
self.content = spec["Content"]
|
|
|
|
|
|
|
|
# optional
|
|
|
|
self.description = spec.get("Description", "")
|
2022-05-19 11:08:02 +00:00
|
|
|
self.compatible_architectures = spec.get("CompatibleArchitectures", [])
|
2021-01-17 15:28:49 +00:00
|
|
|
self.compatible_runtimes = spec.get("CompatibleRuntimes", [])
|
|
|
|
self.license_info = spec.get("LicenseInfo", "")
|
|
|
|
|
|
|
|
# auto-generated
|
|
|
|
self.created_date = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
self.version = None
|
|
|
|
self._attached = False
|
|
|
|
self._layer = None
|
|
|
|
|
|
|
|
if "ZipFile" in self.content:
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _zipfile_content(self.content["ZipFile"])
|
2021-01-17 15:28:49 +00:00
|
|
|
else:
|
|
|
|
key = _validate_s3_bucket_and_key(self.content)
|
|
|
|
if key:
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _s3_content(key)
|
2021-01-17 15:28:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def arn(self):
|
|
|
|
if self.version:
|
2022-05-08 22:25:40 +00:00
|
|
|
return make_layer_ver_arn(
|
|
|
|
self.region, get_account_id(), self.name, self.version
|
|
|
|
)
|
2021-01-17 15:28:49 +00:00
|
|
|
raise ValueError("Layer version is not set")
|
|
|
|
|
|
|
|
def attach(self, layer, version):
|
|
|
|
self._attached = True
|
|
|
|
self._layer = layer
|
|
|
|
self.version = version
|
|
|
|
|
|
|
|
def get_layer_version(self):
|
|
|
|
return {
|
2022-03-17 12:32:31 +00:00
|
|
|
"Content": {
|
|
|
|
"Location": "s3://",
|
|
|
|
"CodeSha256": self.code_sha_256,
|
|
|
|
"CodeSize": self.code_size,
|
|
|
|
},
|
2021-01-17 15:28:49 +00:00
|
|
|
"Version": self.version,
|
2022-03-17 12:32:31 +00:00
|
|
|
"LayerArn": self._layer.layer_arn,
|
2021-01-17 15:28:49 +00:00
|
|
|
"LayerVersionArn": self.arn,
|
|
|
|
"CreatedDate": self.created_date,
|
2022-05-19 11:08:02 +00:00
|
|
|
"CompatibleArchitectures": self.compatible_architectures,
|
2021-01-17 15:28:49 +00:00
|
|
|
"CompatibleRuntimes": self.compatible_runtimes,
|
|
|
|
"Description": self.description,
|
|
|
|
"LicenseInfo": self.license_info,
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_name_type():
|
|
|
|
return "LayerVersion"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_type():
|
|
|
|
return "AWS::Lambda::LayerVersion"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_from_cloudformation_json(
|
2021-11-03 21:00:42 +00:00
|
|
|
cls, resource_name, cloudformation_json, region_name, **kwargs
|
2021-01-17 15:28:49 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
2022-03-10 14:39:59 +00:00
|
|
|
optional_properties = ("Description", "CompatibleRuntimes", "LicenseInfo")
|
2021-01-17 15:28:49 +00:00
|
|
|
|
|
|
|
# required
|
|
|
|
spec = {
|
|
|
|
"Content": properties["Content"],
|
|
|
|
"LayerName": resource_name,
|
|
|
|
}
|
|
|
|
for prop in optional_properties:
|
|
|
|
if prop in properties:
|
|
|
|
spec[prop] = properties[prop]
|
|
|
|
|
|
|
|
backend = lambda_backends[region_name]
|
|
|
|
layer_version = backend.publish_layer_version(spec)
|
|
|
|
return layer_version
|
|
|
|
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
class LambdaAlias(BaseModel):
|
|
|
|
def __init__(
|
|
|
|
self, region, name, function_name, function_version, description, routing_config
|
|
|
|
):
|
2022-05-08 22:25:40 +00:00
|
|
|
self.arn = f"arn:aws:lambda:{region}:{get_account_id()}:function:{function_name}:{name}"
|
2022-03-17 12:32:31 +00:00
|
|
|
self.name = name
|
|
|
|
self.function_version = function_version
|
|
|
|
self.description = description
|
|
|
|
self.routing_config = routing_config
|
|
|
|
self.revision_id = str(uuid4())
|
|
|
|
|
|
|
|
def update(self, description, function_version, routing_config):
|
|
|
|
if description is not None:
|
|
|
|
self.description = description
|
|
|
|
if function_version is not None:
|
|
|
|
self.function_version = function_version
|
|
|
|
if routing_config is not None:
|
|
|
|
self.routing_config = routing_config
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
"AliasArn": self.arn,
|
|
|
|
"Description": self.description,
|
|
|
|
"FunctionVersion": self.function_version,
|
|
|
|
"Name": self.name,
|
|
|
|
"RevisionId": self.revision_id,
|
|
|
|
"RoutingConfig": self.routing_config or None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
class Layer(object):
|
|
|
|
def __init__(self, name, region):
|
|
|
|
self.region = region
|
|
|
|
self.name = name
|
|
|
|
|
2022-05-08 22:25:40 +00:00
|
|
|
self.layer_arn = make_layer_arn(region, get_account_id(), self.name)
|
2021-01-17 15:28:49 +00:00
|
|
|
self._latest_version = 0
|
|
|
|
self.layer_versions = {}
|
|
|
|
|
|
|
|
def attach_version(self, layer_version):
|
|
|
|
self._latest_version += 1
|
|
|
|
layer_version.attach(self, self._latest_version)
|
|
|
|
self.layer_versions[str(self._latest_version)] = layer_version
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def delete_version(self, layer_version):
|
|
|
|
self.layer_versions.pop(str(layer_version), None)
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
"LayerName": self.name,
|
|
|
|
"LayerArn": self.layer_arn,
|
|
|
|
"LatestMatchingVersion": self.layer_versions[
|
|
|
|
str(self._latest_version)
|
|
|
|
].get_layer_version(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-08 15:16:53 +00:00
|
|
|
class LambdaFunction(CloudFormationModel, DockerModel):
|
2022-03-11 21:28:45 +00:00
|
|
|
def __init__(self, spec, region, version=1):
|
2020-11-08 15:16:53 +00:00
|
|
|
DockerModel.__init__(self)
|
2016-02-12 20:38:39 +00:00
|
|
|
# required
|
2017-09-27 23:04:58 +00:00
|
|
|
self.region = region
|
2019-10-31 15:44:26 +00:00
|
|
|
self.code = spec["Code"]
|
|
|
|
self.function_name = spec["FunctionName"]
|
|
|
|
self.handler = spec["Handler"]
|
|
|
|
self.role = spec["Role"]
|
|
|
|
self.run_time = spec["Runtime"]
|
2017-09-27 23:04:58 +00:00
|
|
|
self.logs_backend = logs_backends[self.region]
|
2019-10-31 15:44:26 +00:00
|
|
|
self.environment_vars = spec.get("Environment", {}).get("Variables", {})
|
2020-01-23 18:46:24 +00:00
|
|
|
self.policy = None
|
|
|
|
self.state = "Active"
|
2020-08-26 10:06:53 +00:00
|
|
|
self.reserved_concurrency = spec.get("ReservedConcurrentExecutions", None)
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2016-02-12 20:38:39 +00:00
|
|
|
# optional
|
2019-10-31 15:44:26 +00:00
|
|
|
self.description = spec.get("Description", "")
|
|
|
|
self.memory_size = spec.get("MemorySize", 128)
|
2022-02-08 21:12:51 +00:00
|
|
|
self.package_type = spec.get("PackageType", None)
|
2019-10-31 15:44:26 +00:00
|
|
|
self.publish = spec.get("Publish", False) # this is ignored currently
|
|
|
|
self.timeout = spec.get("Timeout", 3)
|
2021-01-17 15:28:49 +00:00
|
|
|
self.layers = self._get_layers_data(spec.get("Layers", []))
|
2022-02-08 21:12:51 +00:00
|
|
|
self.signing_profile_version_arn = spec.get("SigningProfileVersionArn")
|
|
|
|
self.signing_job_arn = spec.get("SigningJobArn")
|
|
|
|
self.code_signing_config_arn = spec.get("CodeSigningConfigArn")
|
2022-03-17 12:32:31 +00:00
|
|
|
self.tracing_config = spec.get("TracingConfig") or {"Mode": "PassThrough"}
|
2016-02-16 20:35:29 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
self.logs_group_name = "/aws/lambda/{}".format(self.function_name)
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2016-02-16 20:35:29 +00:00
|
|
|
# this isn't finished yet. it needs to find out the VpcId value
|
2017-02-24 02:37:43 +00:00
|
|
|
self._vpc_config = spec.get(
|
2019-10-31 15:44:26 +00:00
|
|
|
"VpcConfig", {"SubnetIds": [], "SecurityGroupIds": []}
|
|
|
|
)
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2016-02-12 20:38:39 +00:00
|
|
|
# auto-generated
|
2017-11-26 21:28:28 +00:00
|
|
|
self.version = version
|
2019-10-31 15:44:26 +00:00
|
|
|
self.last_modified = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
if "ZipFile" in self.code:
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _zipfile_content(self.code["ZipFile"])
|
2017-09-27 23:04:58 +00:00
|
|
|
|
|
|
|
# TODO: we should be putting this in a lambda bucket
|
2019-10-31 15:44:26 +00:00
|
|
|
self.code["UUID"] = str(uuid.uuid4())
|
|
|
|
self.code["S3Key"] = "{}-{}".format(self.function_name, self.code["UUID"])
|
2016-02-16 20:15:34 +00:00
|
|
|
else:
|
2021-01-17 15:28:49 +00:00
|
|
|
key = _validate_s3_bucket_and_key(self.code)
|
2017-05-24 12:54:00 +00:00
|
|
|
if key:
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _s3_content(key)
|
2021-02-15 10:31:33 +00:00
|
|
|
else:
|
|
|
|
self.code_bytes = ""
|
|
|
|
self.code_size = 0
|
|
|
|
self.code_sha_256 = ""
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
self.function_arn = make_function_arn(
|
2022-05-08 22:25:40 +00:00
|
|
|
self.region, get_account_id(), self.function_name
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2021-12-04 11:17:38 +00:00
|
|
|
if spec.get("Tags"):
|
|
|
|
self.tags = spec.get("Tags")
|
|
|
|
else:
|
|
|
|
self.tags = dict()
|
2017-09-13 19:00:39 +00:00
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
self._aliases = dict()
|
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
def set_version(self, version):
|
2019-10-31 15:44:26 +00:00
|
|
|
self.function_arn = make_function_ver_arn(
|
2022-05-08 22:25:40 +00:00
|
|
|
self.region, get_account_id(), self.function_name, version
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-11-26 21:28:28 +00:00
|
|
|
self.version = version
|
2019-10-31 15:44:26 +00:00
|
|
|
self.last_modified = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2016-02-16 20:35:29 +00:00
|
|
|
@property
|
|
|
|
def vpc_config(self):
|
|
|
|
config = self._vpc_config.copy()
|
2019-10-31 15:44:26 +00:00
|
|
|
if config["SecurityGroupIds"]:
|
2016-02-16 20:35:29 +00:00
|
|
|
config.update({"VpcId": "vpc-123abc"})
|
|
|
|
return config
|
|
|
|
|
2019-04-16 03:07:14 +00:00
|
|
|
@property
|
|
|
|
def physical_resource_id(self):
|
|
|
|
return self.function_name
|
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return json.dumps(self.get_configuration())
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def _get_layers_data(self, layers_versions_arns):
|
|
|
|
backend = lambda_backends[self.region]
|
|
|
|
layer_versions = [
|
|
|
|
backend.layers_versions_by_arn(layer_version)
|
|
|
|
for layer_version in layers_versions_arns
|
|
|
|
]
|
|
|
|
if not all(layer_versions):
|
|
|
|
raise ValueError(
|
|
|
|
"InvalidParameterValueException",
|
|
|
|
"One or more LayerVersion does not exist {0}".format(
|
|
|
|
layers_versions_arns
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return [{"Arn": lv.arn, "CodeSize": lv.code_size} for lv in layer_versions]
|
|
|
|
|
2022-02-08 21:12:51 +00:00
|
|
|
def get_code_signing_config(self):
|
|
|
|
return {
|
|
|
|
"CodeSigningConfigArn": self.code_signing_config_arn,
|
|
|
|
"FunctionName": self.function_name,
|
|
|
|
}
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def get_configuration(self, on_create=False):
|
2017-09-27 23:04:58 +00:00
|
|
|
config = {
|
2016-02-12 19:39:20 +00:00
|
|
|
"CodeSha256": self.code_sha_256,
|
|
|
|
"CodeSize": self.code_size,
|
|
|
|
"Description": self.description,
|
|
|
|
"FunctionArn": self.function_arn,
|
|
|
|
"FunctionName": self.function_name,
|
|
|
|
"Handler": self.handler,
|
|
|
|
"LastModified": self.last_modified,
|
|
|
|
"MemorySize": self.memory_size,
|
|
|
|
"Role": self.role,
|
|
|
|
"Runtime": self.run_time,
|
2020-01-23 18:46:24 +00:00
|
|
|
"State": self.state,
|
2022-02-08 21:12:51 +00:00
|
|
|
"PackageType": self.package_type,
|
2016-02-12 19:39:20 +00:00
|
|
|
"Timeout": self.timeout,
|
2017-11-26 21:28:28 +00:00
|
|
|
"Version": str(self.version),
|
2016-02-12 19:39:20 +00:00
|
|
|
"VpcConfig": self.vpc_config,
|
2021-01-17 15:28:49 +00:00
|
|
|
"Layers": self.layers,
|
2022-02-08 21:12:51 +00:00
|
|
|
"SigningProfileVersionArn": self.signing_profile_version_arn,
|
|
|
|
"SigningJobArn": self.signing_job_arn,
|
2022-03-17 12:32:31 +00:00
|
|
|
"TracingConfig": self.tracing_config,
|
2016-02-12 19:39:20 +00:00
|
|
|
}
|
2022-03-17 12:32:31 +00:00
|
|
|
if not on_create:
|
|
|
|
# Only return this variable after the first creation
|
|
|
|
config["LastUpdateStatus"] = "Successful"
|
2017-09-27 23:04:58 +00:00
|
|
|
if self.environment_vars:
|
2019-10-31 15:44:26 +00:00
|
|
|
config["Environment"] = {"Variables": self.environment_vars}
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
def get_code(self):
|
2020-08-26 10:06:53 +00:00
|
|
|
code = {
|
2017-09-27 23:04:58 +00:00
|
|
|
"Code": {
|
2019-10-31 15:44:26 +00:00
|
|
|
"Location": "s3://awslambda-{0}-tasks.s3-{0}.amazonaws.com/{1}".format(
|
|
|
|
self.region, self.code["S3Key"]
|
|
|
|
),
|
|
|
|
"RepositoryType": "S3",
|
2017-09-27 23:04:58 +00:00
|
|
|
},
|
|
|
|
"Configuration": self.get_configuration(),
|
|
|
|
}
|
2022-03-17 12:32:31 +00:00
|
|
|
if self.tags:
|
|
|
|
code["Tags"] = self.tags
|
2020-08-26 10:06:53 +00:00
|
|
|
if self.reserved_concurrency:
|
|
|
|
code.update(
|
|
|
|
{
|
|
|
|
"Concurrency": {
|
|
|
|
"ReservedConcurrentExecutions": self.reserved_concurrency
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return code
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2019-10-08 20:59:03 +00:00
|
|
|
def update_configuration(self, config_updates):
|
|
|
|
for key, value in config_updates.items():
|
|
|
|
if key == "Description":
|
|
|
|
self.description = value
|
|
|
|
elif key == "Handler":
|
|
|
|
self.handler = value
|
|
|
|
elif key == "MemorySize":
|
|
|
|
self.memory_size = value
|
|
|
|
elif key == "Role":
|
|
|
|
self.role = value
|
|
|
|
elif key == "Runtime":
|
|
|
|
self.run_time = value
|
|
|
|
elif key == "Timeout":
|
|
|
|
self.timeout = value
|
|
|
|
elif key == "VpcConfig":
|
2020-11-18 08:45:31 +00:00
|
|
|
self._vpc_config = value
|
2019-11-04 13:44:01 +00:00
|
|
|
elif key == "Environment":
|
|
|
|
self.environment_vars = value["Variables"]
|
2021-01-17 15:28:49 +00:00
|
|
|
elif key == "Layers":
|
|
|
|
self.layers = self._get_layers_data(value)
|
2019-10-08 20:59:03 +00:00
|
|
|
|
|
|
|
return self.get_configuration()
|
|
|
|
|
2019-10-09 20:15:10 +00:00
|
|
|
def update_function_code(self, updated_spec):
|
2019-10-31 15:44:26 +00:00
|
|
|
if "DryRun" in updated_spec and updated_spec["DryRun"]:
|
2019-10-08 20:59:03 +00:00
|
|
|
return self.get_configuration()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
if "ZipFile" in updated_spec:
|
|
|
|
self.code["ZipFile"] = updated_spec["ZipFile"]
|
2019-10-09 20:15:10 +00:00
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _zipfile_content(updated_spec["ZipFile"])
|
2019-10-08 20:59:03 +00:00
|
|
|
|
|
|
|
# TODO: we should be putting this in a lambda bucket
|
2019-10-31 15:44:26 +00:00
|
|
|
self.code["UUID"] = str(uuid.uuid4())
|
|
|
|
self.code["S3Key"] = "{}-{}".format(self.function_name, self.code["UUID"])
|
|
|
|
elif "S3Bucket" in updated_spec and "S3Key" in updated_spec:
|
2019-10-08 20:59:03 +00:00
|
|
|
key = None
|
|
|
|
try:
|
|
|
|
# FIXME: does not validate bucket region
|
2020-06-06 12:15:50 +00:00
|
|
|
key = s3_backend.get_object(
|
2019-10-31 15:44:26 +00:00
|
|
|
updated_spec["S3Bucket"], updated_spec["S3Key"]
|
|
|
|
)
|
2019-10-08 20:59:03 +00:00
|
|
|
except MissingBucket:
|
|
|
|
if do_validate_s3():
|
|
|
|
raise ValueError(
|
|
|
|
"InvalidParameterValueException",
|
2019-10-31 15:44:26 +00:00
|
|
|
"Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist",
|
|
|
|
)
|
2019-10-08 20:59:03 +00:00
|
|
|
except MissingKey:
|
|
|
|
if do_validate_s3():
|
|
|
|
raise ValueError(
|
|
|
|
"InvalidParameterValueException",
|
2019-10-31 15:44:26 +00:00
|
|
|
"Error occurred while GetObject. S3 Error Code: NoSuchKey. S3 Error Message: The specified key does not exist.",
|
|
|
|
)
|
2019-10-08 20:59:03 +00:00
|
|
|
if key:
|
2022-03-17 12:32:31 +00:00
|
|
|
(
|
|
|
|
self.code_bytes,
|
|
|
|
self.code_size,
|
|
|
|
self.code_sha_256,
|
|
|
|
self.code_digest,
|
|
|
|
) = _s3_content(key)
|
2019-11-24 14:54:38 +00:00
|
|
|
self.code["S3Bucket"] = updated_spec["S3Bucket"]
|
|
|
|
self.code["S3Key"] = updated_spec["S3Key"]
|
2019-10-08 20:59:03 +00:00
|
|
|
|
|
|
|
return self.get_configuration()
|
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
@staticmethod
|
|
|
|
def convert(s):
|
2016-10-09 15:13:52 +00:00
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
return str(s, encoding="utf-8")
|
2017-12-08 21:02:34 +00:00
|
|
|
except Exception:
|
2016-10-09 15:13:52 +00:00
|
|
|
return s
|
|
|
|
|
2022-03-11 21:28:45 +00:00
|
|
|
def _invoke_lambda(self, event=None):
|
2021-02-15 10:31:33 +00:00
|
|
|
# Create the LogGroup if necessary, to write the result to
|
|
|
|
self.logs_backend.ensure_log_group(self.logs_group_name, [])
|
2017-09-27 23:04:58 +00:00
|
|
|
# TODO: context not yet implemented
|
|
|
|
if event is None:
|
|
|
|
event = dict()
|
2020-05-07 09:40:24 +00:00
|
|
|
output = None
|
2016-10-06 14:41:31 +00:00
|
|
|
|
2016-10-06 09:52:23 +00:00
|
|
|
try:
|
2017-09-27 23:04:58 +00:00
|
|
|
# TODO: I believe we can keep the container running and feed events as needed
|
|
|
|
# also need to hook it up to the other services so it can make kws/s3 etc calls
|
2020-01-05 11:36:51 +00:00
|
|
|
# Should get invoke_id /RequestId from invocation
|
2017-09-27 23:04:58 +00:00
|
|
|
env_vars = {
|
2020-08-19 21:18:32 +00:00
|
|
|
"_HANDLER": self.handler,
|
|
|
|
"AWS_EXECUTION_ENV": "AWS_Lambda_{}".format(self.run_time),
|
2017-09-27 23:04:58 +00:00
|
|
|
"AWS_LAMBDA_FUNCTION_TIMEOUT": self.timeout,
|
|
|
|
"AWS_LAMBDA_FUNCTION_NAME": self.function_name,
|
|
|
|
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": self.memory_size,
|
|
|
|
"AWS_LAMBDA_FUNCTION_VERSION": self.version,
|
|
|
|
"AWS_REGION": self.region,
|
2020-08-19 21:18:32 +00:00
|
|
|
"AWS_ACCESS_KEY_ID": "role-account-id",
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "role-secret-key",
|
|
|
|
"AWS_SESSION_TOKEN": "session-token",
|
2017-09-27 23:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
env_vars.update(self.environment_vars)
|
2022-01-27 12:04:03 +00:00
|
|
|
env_vars["MOTO_HOST"] = settings.moto_server_host()
|
|
|
|
env_vars["MOTO_PORT"] = settings.moto_server_port()
|
|
|
|
env_vars[
|
|
|
|
"MOTO_HTTP_ENDPOINT"
|
|
|
|
] = f'{env_vars["MOTO_HOST"]}:{env_vars["MOTO_PORT"]}'
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2020-05-07 09:40:24 +00:00
|
|
|
container = exit_code = None
|
2020-01-23 00:58:25 +00:00
|
|
|
log_config = docker.types.LogConfig(type=docker.types.LogConfig.types.JSON)
|
2021-08-28 09:48:28 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
with _DockerDataVolumeContext(self) as data_vol:
|
|
|
|
try:
|
2022-01-27 12:04:03 +00:00
|
|
|
run_kwargs = dict()
|
|
|
|
network_name = settings.moto_network_name()
|
|
|
|
network_mode = settings.moto_network_mode()
|
|
|
|
if network_name:
|
|
|
|
run_kwargs["network"] = network_name
|
|
|
|
elif network_mode:
|
|
|
|
run_kwargs["network_mode"] = network_mode
|
|
|
|
elif settings.TEST_SERVER_MODE:
|
|
|
|
# AWSLambda can make HTTP requests to a Docker container called 'motoserver'
|
|
|
|
# Only works if our Docker-container is named 'motoserver'
|
|
|
|
# TODO: should remove this and rely on 'network_mode' instead, as this is too tightly coupled with our own test setup
|
|
|
|
run_kwargs["links"] = {"motoserver": "motoserver"}
|
|
|
|
|
2021-10-13 21:35:38 +00:00
|
|
|
# add host.docker.internal host on linux to emulate Mac + Windows behavior
|
|
|
|
# for communication with other mock AWS services running on localhost
|
|
|
|
if platform == "linux" or platform == "linux2":
|
|
|
|
run_kwargs["extra_hosts"] = {
|
|
|
|
"host.docker.internal": "host-gateway"
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:53:43 +00:00
|
|
|
image_repo = settings.moto_lambda_image()
|
|
|
|
image_ref = f"{image_repo}:{self.run_time}"
|
2021-02-18 08:58:20 +00:00
|
|
|
self.docker_client.images.pull(":".join(parse_image_ref(image_ref)))
|
2017-09-27 23:04:58 +00:00
|
|
|
container = self.docker_client.containers.run(
|
2021-02-18 08:58:20 +00:00
|
|
|
image_ref,
|
2019-10-31 15:44:26 +00:00
|
|
|
[self.handler, json.dumps(event)],
|
|
|
|
remove=False,
|
2017-09-27 23:04:58 +00:00
|
|
|
mem_limit="{}m".format(self.memory_size),
|
2019-10-31 15:44:26 +00:00
|
|
|
volumes=["{}:/var/task".format(data_vol.name)],
|
|
|
|
environment=env_vars,
|
|
|
|
detach=True,
|
2020-01-23 00:58:25 +00:00
|
|
|
log_config=log_config,
|
2022-01-27 12:04:03 +00:00
|
|
|
**run_kwargs,
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-09-27 23:04:58 +00:00
|
|
|
finally:
|
|
|
|
if container:
|
2017-10-25 19:04:00 +00:00
|
|
|
try:
|
2018-06-01 02:39:22 +00:00
|
|
|
exit_code = container.wait(timeout=300)
|
2017-10-25 19:04:00 +00:00
|
|
|
except requests.exceptions.ReadTimeout:
|
|
|
|
exit_code = -1
|
|
|
|
container.stop()
|
|
|
|
container.kill()
|
2018-06-01 02:39:22 +00:00
|
|
|
else:
|
|
|
|
if docker_3:
|
2019-10-31 15:44:26 +00:00
|
|
|
exit_code = exit_code["StatusCode"]
|
2018-06-01 02:39:22 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
output = container.logs(stdout=False, stderr=True)
|
|
|
|
output += container.logs(stdout=True, stderr=False)
|
|
|
|
container.remove()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
output = output.decode("utf-8")
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2021-08-28 09:48:28 +00:00
|
|
|
self.save_logs(output)
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2020-01-05 11:36:51 +00:00
|
|
|
# We only care about the response from the lambda
|
|
|
|
# Which is the last line of the output, according to https://github.com/lambci/docker-lambda/issues/25
|
2020-05-07 09:40:24 +00:00
|
|
|
resp = output.splitlines()[-1]
|
|
|
|
logs = os.linesep.join(
|
|
|
|
[line for line in self.convert(output).splitlines()[:-1]]
|
|
|
|
)
|
2021-08-28 08:41:05 +00:00
|
|
|
invocation_error = exit_code != 0
|
|
|
|
return resp, invocation_error, logs
|
2020-07-20 10:37:45 +00:00
|
|
|
except docker.errors.DockerException as e:
|
|
|
|
# Docker itself is probably not running - there will be no Lambda-logs to handle
|
2021-08-28 09:48:28 +00:00
|
|
|
msg = "error running docker: {}".format(e)
|
|
|
|
self.save_logs(msg)
|
|
|
|
return msg, True, ""
|
|
|
|
|
|
|
|
def save_logs(self, output):
|
|
|
|
# Send output to "logs" backend
|
|
|
|
invoke_id = uuid.uuid4().hex
|
2022-03-10 14:39:59 +00:00
|
|
|
log_stream_name = (
|
|
|
|
"{date.year}/{date.month:02d}/{date.day:02d}/[{version}]{invoke_id}".format(
|
|
|
|
date=datetime.datetime.utcnow(),
|
|
|
|
version=self.version,
|
|
|
|
invoke_id=invoke_id,
|
|
|
|
)
|
2021-08-28 09:48:28 +00:00
|
|
|
)
|
|
|
|
self.logs_backend.create_log_stream(self.logs_group_name, log_stream_name)
|
|
|
|
log_events = [
|
|
|
|
{"timestamp": unix_time_millis(), "message": line}
|
|
|
|
for line in output.splitlines()
|
|
|
|
]
|
|
|
|
self.logs_backend.put_log_events(
|
2022-03-11 21:28:45 +00:00
|
|
|
self.logs_group_name, log_stream_name, log_events
|
2021-08-28 09:48:28 +00:00
|
|
|
)
|
2016-10-06 09:52:23 +00:00
|
|
|
|
2017-02-24 00:43:48 +00:00
|
|
|
def invoke(self, body, request_headers, response_headers):
|
2016-06-22 19:24:46 +00:00
|
|
|
|
2017-09-27 23:04:58 +00:00
|
|
|
if body:
|
|
|
|
body = json.loads(body)
|
2021-11-03 21:00:42 +00:00
|
|
|
else:
|
|
|
|
body = "{}"
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2016-06-22 19:24:46 +00:00
|
|
|
# Get the invocation type:
|
2022-03-11 21:28:45 +00:00
|
|
|
res, errored, logs = self._invoke_lambda(event=body)
|
2021-01-26 13:28:01 +00:00
|
|
|
inv_type = request_headers.get("x-amz-invocation-type", "RequestResponse")
|
|
|
|
if inv_type == "RequestResponse":
|
2020-05-07 09:40:24 +00:00
|
|
|
encoded = base64.b64encode(logs.encode("utf-8"))
|
2019-10-31 15:44:26 +00:00
|
|
|
response_headers["x-amz-log-result"] = encoded.decode("utf-8")
|
|
|
|
result = res.encode("utf-8")
|
2016-12-03 23:17:15 +00:00
|
|
|
else:
|
2020-01-23 00:39:11 +00:00
|
|
|
result = res
|
2017-03-17 02:00:57 +00:00
|
|
|
if errored:
|
2019-10-31 15:44:26 +00:00
|
|
|
response_headers["x-amz-function-error"] = "Handled"
|
2016-06-22 19:24:46 +00:00
|
|
|
|
2016-12-03 23:17:15 +00:00
|
|
|
return result
|
2016-06-22 19:24:46 +00:00
|
|
|
|
2020-08-01 14:23:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def cloudformation_name_type():
|
|
|
|
return "FunctionName"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_type():
|
|
|
|
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html
|
|
|
|
return "AWS::Lambda::Function"
|
|
|
|
|
2016-02-12 20:38:39 +00:00
|
|
|
@classmethod
|
2019-10-31 15:44:26 +00:00
|
|
|
def create_from_cloudformation_json(
|
2021-11-03 21:00:42 +00:00
|
|
|
cls, resource_name, cloudformation_json, region_name, **kwargs
|
2019-10-31 15:44:26 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
2020-08-26 10:06:53 +00:00
|
|
|
optional_properties = (
|
|
|
|
"Description",
|
|
|
|
"MemorySize",
|
|
|
|
"Publish",
|
|
|
|
"Timeout",
|
|
|
|
"VpcConfig",
|
|
|
|
"Environment",
|
|
|
|
"ReservedConcurrentExecutions",
|
|
|
|
)
|
2016-02-12 20:38:39 +00:00
|
|
|
|
2016-02-16 22:07:35 +00:00
|
|
|
# required
|
|
|
|
spec = {
|
2019-10-31 15:44:26 +00:00
|
|
|
"Code": properties["Code"],
|
|
|
|
"FunctionName": resource_name,
|
|
|
|
"Handler": properties["Handler"],
|
|
|
|
"Role": properties["Role"],
|
|
|
|
"Runtime": properties["Runtime"],
|
2016-02-16 22:07:35 +00:00
|
|
|
}
|
2020-08-26 10:06:53 +00:00
|
|
|
|
2017-02-24 02:37:43 +00:00
|
|
|
# NOTE: Not doing `properties.get(k, DEFAULT)` to avoid duplicating the
|
|
|
|
# default logic
|
2016-02-16 22:07:35 +00:00
|
|
|
for prop in optional_properties:
|
|
|
|
if prop in properties:
|
|
|
|
spec[prop] = properties[prop]
|
2016-02-12 20:38:39 +00:00
|
|
|
|
2017-05-17 22:16:40 +00:00
|
|
|
# when ZipFile is present in CloudFormation, per the official docs,
|
|
|
|
# the code it's a plaintext code snippet up to 4096 bytes.
|
|
|
|
# this snippet converts this plaintext code to a proper base64-encoded ZIP file.
|
2019-10-31 15:44:26 +00:00
|
|
|
if "ZipFile" in properties["Code"]:
|
|
|
|
spec["Code"]["ZipFile"] = base64.b64encode(
|
|
|
|
cls._create_zipfile_from_plaintext_code(spec["Code"]["ZipFile"])
|
|
|
|
)
|
2017-05-17 22:16:40 +00:00
|
|
|
|
2016-02-16 22:07:35 +00:00
|
|
|
backend = lambda_backends[region_name]
|
|
|
|
fn = backend.create_function(spec)
|
2016-02-12 20:38:39 +00:00
|
|
|
return fn
|
|
|
|
|
2021-11-03 21:00:42 +00:00
|
|
|
@classmethod
|
2022-03-11 21:28:45 +00:00
|
|
|
def has_cfn_attr(cls, attr):
|
|
|
|
return attr in ["Arn"]
|
2021-11-03 21:00:42 +00:00
|
|
|
|
2017-05-24 12:54:00 +00:00
|
|
|
def get_cfn_attribute(self, attribute_name):
|
2019-10-31 15:44:26 +00:00
|
|
|
from moto.cloudformation.exceptions import UnformattedGetAttTemplateException
|
|
|
|
|
|
|
|
if attribute_name == "Arn":
|
2022-05-08 22:25:40 +00:00
|
|
|
return make_function_arn(self.region, get_account_id(), self.function_name)
|
2017-05-24 12:54:00 +00:00
|
|
|
raise UnformattedGetAttTemplateException()
|
|
|
|
|
2019-11-24 14:54:38 +00:00
|
|
|
@classmethod
|
|
|
|
def update_from_cloudformation_json(
|
2022-03-11 21:28:45 +00:00
|
|
|
cls, original_resource, new_resource_name, cloudformation_json, region_name
|
2019-11-24 14:54:38 +00:00
|
|
|
):
|
|
|
|
updated_props = cloudformation_json["Properties"]
|
|
|
|
original_resource.update_configuration(updated_props)
|
|
|
|
original_resource.update_function_code(updated_props["Code"])
|
|
|
|
return original_resource
|
|
|
|
|
2017-05-17 22:16:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def _create_zipfile_from_plaintext_code(code):
|
|
|
|
zip_output = io.BytesIO()
|
2019-10-31 15:44:26 +00:00
|
|
|
zip_file = zipfile.ZipFile(zip_output, "w", zipfile.ZIP_DEFLATED)
|
2021-11-03 21:00:42 +00:00
|
|
|
zip_file.writestr("index.py", code)
|
|
|
|
# This should really be part of the 'lambci' docker image
|
|
|
|
from moto.packages.cfnresponse import cfnresponse
|
|
|
|
|
|
|
|
with open(cfnresponse.__file__) as cfn:
|
|
|
|
zip_file.writestr("cfnresponse.py", cfn.read())
|
2017-05-17 22:16:40 +00:00
|
|
|
zip_file.close()
|
|
|
|
zip_output.seek(0)
|
|
|
|
return zip_output.read()
|
|
|
|
|
2019-11-24 14:54:38 +00:00
|
|
|
def delete(self, region):
|
|
|
|
lambda_backends[region].delete_function(self.function_name)
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def delete_alias(self, name):
|
|
|
|
self._aliases.pop(name, None)
|
|
|
|
|
|
|
|
def get_alias(self, name):
|
|
|
|
if name in self._aliases:
|
|
|
|
return self._aliases[name]
|
2022-05-08 22:25:40 +00:00
|
|
|
arn = f"arn:aws:lambda:{self.region}:{get_account_id()}:function:{self.function_name}:{name}"
|
2022-03-17 12:32:31 +00:00
|
|
|
raise UnknownAliasException(arn)
|
|
|
|
|
|
|
|
def put_alias(self, name, description, function_version, routing_config):
|
|
|
|
alias = LambdaAlias(
|
|
|
|
region=self.region,
|
|
|
|
name=name,
|
|
|
|
function_name=self.function_name,
|
|
|
|
function_version=function_version,
|
|
|
|
description=description,
|
|
|
|
routing_config=routing_config,
|
|
|
|
)
|
|
|
|
self._aliases[name] = alias
|
|
|
|
return alias
|
|
|
|
|
|
|
|
def update_alias(self, name, description, function_version, routing_config):
|
|
|
|
alias = self.get_alias(name)
|
|
|
|
alias.update(description, function_version, routing_config)
|
|
|
|
return alias
|
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2020-08-01 14:23:36 +00:00
|
|
|
class EventSourceMapping(CloudFormationModel):
|
2017-05-24 12:54:00 +00:00
|
|
|
def __init__(self, spec):
|
|
|
|
# required
|
2020-06-14 15:03:00 +00:00
|
|
|
self.function_name = spec["FunctionName"]
|
2019-10-31 15:44:26 +00:00
|
|
|
self.event_source_arn = spec["EventSourceArn"]
|
2020-06-14 15:03:00 +00:00
|
|
|
|
|
|
|
# optional
|
|
|
|
self.batch_size = spec.get("BatchSize")
|
|
|
|
self.starting_position = spec.get("StartingPosition", "TRIM_HORIZON")
|
|
|
|
self.enabled = spec.get("Enabled", True)
|
|
|
|
self.starting_position_timestamp = spec.get("StartingPositionTimestamp", None)
|
|
|
|
|
|
|
|
self.function_arn = spec["FunctionArn"]
|
2019-08-21 01:54:57 +00:00
|
|
|
self.uuid = str(uuid.uuid4())
|
|
|
|
self.last_modified = time.mktime(datetime.datetime.utcnow().timetuple())
|
2019-08-21 21:45:37 +00:00
|
|
|
|
2020-06-14 15:03:00 +00:00
|
|
|
def _get_service_source_from_arn(self, event_source_arn):
|
|
|
|
return event_source_arn.split(":")[2].lower()
|
|
|
|
|
|
|
|
def _validate_event_source(self, event_source_arn):
|
|
|
|
valid_services = ("dynamodb", "kinesis", "sqs")
|
|
|
|
service = self._get_service_source_from_arn(event_source_arn)
|
|
|
|
return True if service in valid_services else False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def event_source_arn(self):
|
|
|
|
return self._event_source_arn
|
|
|
|
|
|
|
|
@event_source_arn.setter
|
|
|
|
def event_source_arn(self, event_source_arn):
|
|
|
|
if not self._validate_event_source(event_source_arn):
|
|
|
|
raise ValueError(
|
|
|
|
"InvalidParameterValueException", "Unsupported event source type"
|
|
|
|
)
|
|
|
|
self._event_source_arn = event_source_arn
|
|
|
|
|
|
|
|
@property
|
|
|
|
def batch_size(self):
|
|
|
|
return self._batch_size
|
|
|
|
|
|
|
|
@batch_size.setter
|
|
|
|
def batch_size(self, batch_size):
|
|
|
|
batch_size_service_map = {
|
2019-10-31 15:44:26 +00:00
|
|
|
"kinesis": (100, 10000),
|
|
|
|
"dynamodb": (100, 1000),
|
|
|
|
"sqs": (10, 10),
|
2019-08-21 21:45:37 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:03:00 +00:00
|
|
|
source_type = self._get_service_source_from_arn(self.event_source_arn)
|
|
|
|
batch_size_for_source = batch_size_service_map[source_type]
|
|
|
|
|
|
|
|
if batch_size is None:
|
|
|
|
self._batch_size = batch_size_for_source[0]
|
|
|
|
elif batch_size > batch_size_for_source[1]:
|
|
|
|
error_message = "BatchSize {} exceeds the max of {}".format(
|
|
|
|
batch_size, batch_size_for_source[1]
|
|
|
|
)
|
|
|
|
raise ValueError("InvalidParameterValueException", error_message)
|
|
|
|
else:
|
|
|
|
self._batch_size = int(batch_size)
|
2017-05-24 12:54:00 +00:00
|
|
|
|
2019-08-21 01:54:57 +00:00
|
|
|
def get_configuration(self):
|
|
|
|
return {
|
2019-10-31 15:44:26 +00:00
|
|
|
"UUID": self.uuid,
|
|
|
|
"BatchSize": self.batch_size,
|
|
|
|
"EventSourceArn": self.event_source_arn,
|
|
|
|
"FunctionArn": self.function_arn,
|
|
|
|
"LastModified": self.last_modified,
|
|
|
|
"LastProcessingResult": "",
|
|
|
|
"State": "Enabled" if self.enabled else "Disabled",
|
|
|
|
"StateTransitionReason": "User initiated",
|
2019-08-21 01:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 15:03:00 +00:00
|
|
|
def delete(self, region_name):
|
|
|
|
lambda_backend = lambda_backends[region_name]
|
|
|
|
lambda_backend.delete_event_source_mapping(self.uuid)
|
|
|
|
|
2020-08-01 14:23:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def cloudformation_name_type():
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_type():
|
|
|
|
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html
|
|
|
|
return "AWS::Lambda::EventSourceMapping"
|
|
|
|
|
2017-05-24 12:54:00 +00:00
|
|
|
@classmethod
|
2019-10-31 15:44:26 +00:00
|
|
|
def create_from_cloudformation_json(
|
2021-11-03 21:00:42 +00:00
|
|
|
cls, resource_name, cloudformation_json, region_name, **kwargs
|
2019-10-31 15:44:26 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
2020-06-14 15:03:00 +00:00
|
|
|
lambda_backend = lambda_backends[region_name]
|
|
|
|
return lambda_backend.create_event_source_mapping(properties)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def update_from_cloudformation_json(
|
2022-03-11 21:28:45 +00:00
|
|
|
cls, original_resource, new_resource_name, cloudformation_json, region_name
|
2020-06-14 15:03:00 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
|
|
|
event_source_uuid = original_resource.uuid
|
|
|
|
lambda_backend = lambda_backends[region_name]
|
|
|
|
return lambda_backend.update_event_source_mapping(event_source_uuid, properties)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def delete_from_cloudformation_json(
|
|
|
|
cls, resource_name, cloudformation_json, region_name
|
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
|
|
|
lambda_backend = lambda_backends[region_name]
|
|
|
|
esms = lambda_backend.list_event_source_mappings(
|
|
|
|
event_source_arn=properties["EventSourceArn"],
|
|
|
|
function_name=properties["FunctionName"],
|
|
|
|
)
|
|
|
|
|
|
|
|
for esm in esms:
|
2020-08-27 09:11:47 +00:00
|
|
|
if esm.uuid == resource_name:
|
2020-06-14 15:03:00 +00:00
|
|
|
esm.delete(region_name)
|
2017-05-24 12:54:00 +00:00
|
|
|
|
2020-08-27 09:11:47 +00:00
|
|
|
@property
|
|
|
|
def physical_resource_id(self):
|
|
|
|
return self.uuid
|
|
|
|
|
2017-05-24 12:54:00 +00:00
|
|
|
|
2020-08-01 14:23:36 +00:00
|
|
|
class LambdaVersion(CloudFormationModel):
|
2017-05-24 12:54:00 +00:00
|
|
|
def __init__(self, spec):
|
2019-10-31 15:44:26 +00:00
|
|
|
self.version = spec["Version"]
|
2017-05-24 12:54:00 +00:00
|
|
|
|
2018-05-15 18:13:01 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self.logical_resource_id)
|
|
|
|
|
2020-08-01 14:23:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def cloudformation_name_type():
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def cloudformation_type():
|
|
|
|
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-version.html
|
|
|
|
return "AWS::Lambda::Version"
|
|
|
|
|
2017-05-24 12:54:00 +00:00
|
|
|
@classmethod
|
2019-10-31 15:44:26 +00:00
|
|
|
def create_from_cloudformation_json(
|
2021-11-03 21:00:42 +00:00
|
|
|
cls, resource_name, cloudformation_json, region_name, **kwargs
|
2019-10-31 15:44:26 +00:00
|
|
|
):
|
|
|
|
properties = cloudformation_json["Properties"]
|
|
|
|
function_name = properties["FunctionName"]
|
2019-08-21 01:54:57 +00:00
|
|
|
func = lambda_backends[region_name].publish_function(function_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
spec = {"Version": func.version}
|
2017-05-24 12:54:00 +00:00
|
|
|
return LambdaVersion(spec)
|
|
|
|
|
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
class LambdaStorage(object):
|
2022-03-15 15:07:01 +00:00
|
|
|
def __init__(self, region_name):
|
2022-03-17 12:32:31 +00:00
|
|
|
# Format 'func_name' {'versions': []}
|
2017-11-26 21:28:28 +00:00
|
|
|
self._functions = {}
|
2022-03-17 12:32:31 +00:00
|
|
|
self._aliases = dict()
|
2017-11-26 21:28:28 +00:00
|
|
|
self._arns = weakref.WeakValueDictionary()
|
2022-03-15 15:07:01 +00:00
|
|
|
self.region_name = region_name
|
2017-11-26 21:28:28 +00:00
|
|
|
|
|
|
|
def _get_latest(self, name):
|
2019-10-31 15:44:26 +00:00
|
|
|
return self._functions[name]["latest"]
|
2017-11-26 21:28:28 +00:00
|
|
|
|
|
|
|
def _get_version(self, name, version):
|
|
|
|
index = version - 1
|
|
|
|
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
return self._functions[name]["versions"][index]
|
2017-11-26 21:28:28 +00:00
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def delete_alias(self, name, function_name):
|
|
|
|
fn = self.get_function_by_name_or_arn(function_name)
|
|
|
|
return fn.delete_alias(name)
|
|
|
|
|
|
|
|
def get_alias(self, name, function_name):
|
|
|
|
fn = self.get_function_by_name_or_arn(function_name)
|
|
|
|
return fn.get_alias(name)
|
|
|
|
|
|
|
|
def put_alias(
|
|
|
|
self, name, function_name, function_version, description, routing_config
|
|
|
|
):
|
|
|
|
fn = self.get_function_by_name_or_arn(function_name)
|
|
|
|
return fn.put_alias(name, description, function_version, routing_config)
|
|
|
|
|
|
|
|
def update_alias(
|
|
|
|
self, name, function_name, function_version, description, routing_config
|
|
|
|
):
|
|
|
|
fn = self.get_function_by_name_or_arn(function_name)
|
|
|
|
return fn.update_alias(name, description, function_version, routing_config)
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2019-11-04 13:44:01 +00:00
|
|
|
def get_function_by_name(self, name, qualifier=None):
|
2017-11-26 21:28:28 +00:00
|
|
|
if name not in self._functions:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if qualifier is None:
|
|
|
|
return self._get_latest(name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return self._get_version(name, int(qualifier))
|
|
|
|
except ValueError:
|
2019-10-31 15:44:26 +00:00
|
|
|
return self._functions[name]["latest"]
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2019-02-16 15:27:23 +00:00
|
|
|
def list_versions_by_function(self, name):
|
|
|
|
if name not in self._functions:
|
|
|
|
return None
|
2019-05-21 16:49:56 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
latest = copy.copy(self._functions[name]["latest"])
|
|
|
|
latest.function_arn += ":$LATEST"
|
|
|
|
return [latest] + self._functions[name]["versions"]
|
2019-02-16 15:27:23 +00:00
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
def get_arn(self, arn):
|
2022-03-17 12:32:31 +00:00
|
|
|
# Function ARN may contain an alias
|
|
|
|
# arn:aws:lambda:region:account_id:function:<fn_name>:<alias_name>
|
|
|
|
if ":" in arn.split(":function:")[-1]:
|
|
|
|
# arn = arn:aws:lambda:region:account_id:function:<fn_name>
|
|
|
|
arn = ":".join(arn.split(":")[0:-1])
|
2017-11-26 21:28:28 +00:00
|
|
|
return self._arns.get(arn, None)
|
|
|
|
|
2021-12-01 23:06:58 +00:00
|
|
|
def get_function_by_name_or_arn(self, name_or_arn, qualifier=None):
|
2022-03-19 13:00:39 +00:00
|
|
|
fn = self.get_function_by_name(name_or_arn, qualifier) or self.get_arn(
|
2021-12-01 23:06:58 +00:00
|
|
|
name_or_arn
|
|
|
|
)
|
2022-03-19 13:00:39 +00:00
|
|
|
if fn is None:
|
|
|
|
if name_or_arn.startswith("arn:aws"):
|
|
|
|
arn = name_or_arn
|
|
|
|
else:
|
2022-05-08 22:25:40 +00:00
|
|
|
arn = make_function_arn(self.region_name, get_account_id(), name_or_arn)
|
2022-03-19 13:00:39 +00:00
|
|
|
if qualifier:
|
|
|
|
arn = f"{arn}:{qualifier}"
|
|
|
|
raise UnknownFunctionException(arn)
|
|
|
|
return fn
|
2019-08-21 01:54:57 +00:00
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
def put_function(self, fn):
|
|
|
|
"""
|
|
|
|
:param fn: Function
|
|
|
|
:type fn: LambdaFunction
|
|
|
|
"""
|
2019-11-17 10:59:20 +00:00
|
|
|
valid_role = re.match(InvalidRoleFormat.pattern, fn.role)
|
2019-11-07 17:11:13 +00:00
|
|
|
if valid_role:
|
|
|
|
account = valid_role.group(2)
|
2022-05-08 22:25:40 +00:00
|
|
|
if account != get_account_id():
|
2019-11-17 10:59:20 +00:00
|
|
|
raise CrossAccountNotAllowed()
|
2019-11-07 17:11:13 +00:00
|
|
|
try:
|
|
|
|
iam_backend.get_role_by_arn(fn.role)
|
|
|
|
except IAMNotFoundException:
|
2019-11-17 10:59:20 +00:00
|
|
|
raise InvalidParameterValueException(
|
|
|
|
"The role defined for the function cannot be assumed by Lambda."
|
2019-11-07 17:11:13 +00:00
|
|
|
)
|
|
|
|
else:
|
2019-11-17 10:59:20 +00:00
|
|
|
raise InvalidRoleFormat(fn.role)
|
2017-11-26 21:28:28 +00:00
|
|
|
if fn.function_name in self._functions:
|
2019-10-31 15:44:26 +00:00
|
|
|
self._functions[fn.function_name]["latest"] = fn
|
2017-11-26 21:28:28 +00:00
|
|
|
else:
|
2022-03-17 12:32:31 +00:00
|
|
|
self._functions[fn.function_name] = {"latest": fn, "versions": []}
|
2020-01-23 18:46:24 +00:00
|
|
|
# instantiate a new policy for this version of the lambda
|
|
|
|
fn.policy = Policy(fn)
|
2017-11-26 21:28:28 +00:00
|
|
|
self._arns[fn.function_arn] = fn
|
|
|
|
|
2021-08-28 14:26:44 +00:00
|
|
|
def publish_function(self, name_or_arn, description=""):
|
2021-08-28 06:23:44 +00:00
|
|
|
function = self.get_function_by_name_or_arn(name_or_arn)
|
|
|
|
name = function.function_name
|
2017-11-26 21:28:28 +00:00
|
|
|
if name not in self._functions:
|
|
|
|
return None
|
2019-10-31 15:44:26 +00:00
|
|
|
if not self._functions[name]["latest"]:
|
2017-11-26 21:28:28 +00:00
|
|
|
return None
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
new_version = len(self._functions[name]["versions"]) + 1
|
|
|
|
fn = copy.copy(self._functions[name]["latest"])
|
2017-11-26 21:28:28 +00:00
|
|
|
fn.set_version(new_version)
|
2021-08-28 14:26:44 +00:00
|
|
|
if description:
|
|
|
|
fn.description = description
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
self._functions[name]["versions"].append(fn)
|
2019-05-21 16:49:56 +00:00
|
|
|
self._arns[fn.function_arn] = fn
|
2017-11-26 21:28:28 +00:00
|
|
|
return fn
|
|
|
|
|
2019-10-22 08:31:37 +00:00
|
|
|
def del_function(self, name_or_arn, qualifier=None):
|
2022-03-19 13:00:39 +00:00
|
|
|
function = self.get_function_by_name_or_arn(name_or_arn, qualifier)
|
|
|
|
name = function.function_name
|
|
|
|
if not qualifier:
|
|
|
|
# Something is still reffing this so delete all arns
|
|
|
|
latest = self._functions[name]["latest"].function_arn
|
|
|
|
del self._arns[latest]
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2022-03-19 13:00:39 +00:00
|
|
|
for fn in self._functions[name]["versions"]:
|
|
|
|
del self._arns[fn.function_arn]
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2022-03-19 13:00:39 +00:00
|
|
|
del self._functions[name]
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2022-03-19 13:00:39 +00:00
|
|
|
elif qualifier == "$LATEST":
|
|
|
|
self._functions[name]["latest"] = None
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2022-03-19 13:00:39 +00:00
|
|
|
# If theres no functions left
|
|
|
|
if (
|
|
|
|
not self._functions[name]["versions"]
|
|
|
|
and not self._functions[name]["latest"]
|
|
|
|
):
|
|
|
|
del self._functions[name]
|
|
|
|
|
|
|
|
else:
|
|
|
|
fn = self.get_function_by_name(name, qualifier)
|
|
|
|
if fn:
|
|
|
|
self._functions[name]["versions"].remove(fn)
|
2017-11-26 21:28:28 +00:00
|
|
|
|
|
|
|
# If theres no functions left
|
2019-10-31 15:44:26 +00:00
|
|
|
if (
|
|
|
|
not self._functions[name]["versions"]
|
|
|
|
and not self._functions[name]["latest"]
|
|
|
|
):
|
2017-11-26 21:28:28 +00:00
|
|
|
del self._functions[name]
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for function_group in self._functions.values():
|
2021-08-28 14:26:44 +00:00
|
|
|
latest = copy.deepcopy(function_group["latest"])
|
|
|
|
latest.function_arn = "{}:$LATEST".format(latest.function_arn)
|
|
|
|
result.append(latest)
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
result.extend(function_group["versions"])
|
2017-11-26 21:28:28 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2021-08-28 14:26:44 +00:00
|
|
|
def latest(self):
|
|
|
|
"""
|
|
|
|
Return the list of functions with version @LATEST
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
for function_group in self._functions.values():
|
|
|
|
if function_group["latest"] is not None:
|
|
|
|
result.append(function_group["latest"])
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
class LayerStorage(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._layers = {}
|
|
|
|
self._arns = weakref.WeakValueDictionary()
|
|
|
|
|
|
|
|
def put_layer_version(self, layer_version):
|
|
|
|
"""
|
|
|
|
:param layer_version: LayerVersion
|
|
|
|
"""
|
|
|
|
if layer_version.name not in self._layers:
|
|
|
|
self._layers[layer_version.name] = Layer(
|
|
|
|
layer_version.name, layer_version.region
|
|
|
|
)
|
|
|
|
self._layers[layer_version.name].attach_version(layer_version)
|
|
|
|
|
|
|
|
def list_layers(self):
|
|
|
|
return [layer.to_dict() for layer in self._layers.values()]
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def delete_layer_version(self, layer_name, layer_version):
|
|
|
|
self._layers[layer_name].delete_version(layer_version)
|
|
|
|
|
|
|
|
def get_layer_version(self, layer_name, layer_version):
|
|
|
|
if layer_name not in self._layers:
|
|
|
|
raise UnknownLayerException()
|
|
|
|
for lv in self._layers[layer_name].layer_versions.values():
|
|
|
|
if lv.version == int(layer_version):
|
|
|
|
return lv
|
|
|
|
raise UnknownLayerException()
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def get_layer_versions(self, layer_name):
|
|
|
|
if layer_name in self._layers:
|
|
|
|
return list(iter(self._layers[layer_name].layer_versions.values()))
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_layer_version_by_arn(self, layer_version_arn):
|
|
|
|
split_arn = split_layer_arn(layer_version_arn)
|
|
|
|
if split_arn.layer_name in self._layers:
|
|
|
|
return self._layers[split_arn.layer_name].layer_versions.get(
|
|
|
|
split_arn.version, None
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
class LambdaBackend(BaseBackend):
|
2022-01-27 12:04:03 +00:00
|
|
|
"""
|
2022-03-10 14:39:59 +00:00
|
|
|
Implementation of the AWS Lambda endpoint.
|
|
|
|
Invoking functions is supported - they will run inside a Docker container, emulating the real AWS behaviour as closely as possible.
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
It is possible to connect from AWS Lambdas to other services, as long as you are running Moto in ServerMode.
|
|
|
|
The Lambda has access to environment variables `MOTO_HOST` and `MOTO_PORT`, which can be used to build the url that MotoServer runs on:
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
.. sourcecode:: python
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
def lambda_handler(event, context):
|
|
|
|
host = os.environ.get("MOTO_HOST")
|
|
|
|
port = os.environ.get("MOTO_PORT")
|
|
|
|
url = host + ":" + port
|
|
|
|
ec2 = boto3.client('ec2', region_name='us-west-2', endpoint_url=url)
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
# Or even simpler:
|
|
|
|
full_url = os.environ.get("MOTO_HTTP_ENDPOINT")
|
|
|
|
ec2 = boto3.client("ec2", region_name="eu-west-1", endpoint_url=full_url)
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
ec2.do_whatever_inside_the_existing_moto_server()
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
Moto will run on port 5000 by default. This can be overwritten by setting an environment variable when starting Moto:
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
.. sourcecode:: bash
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
# This env var will be propagated to the Docker container running the Lambda functions
|
|
|
|
MOTO_PORT=5000 moto_server
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
The Docker container uses the default network mode, `bridge`.
|
|
|
|
The following environment variables are available for fine-grained control over the Docker connection options:
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
.. sourcecode:: bash
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
# Provide the name of a custom network to connect to
|
|
|
|
MOTO_DOCKER_NETWORK_NAME=mycustomnetwork moto_server
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
# Override the network mode
|
|
|
|
# For example, network_mode=host would use the network of the host machine
|
|
|
|
# Note that this option will be ignored if MOTO_DOCKER_NETWORK_NAME is also set
|
|
|
|
MOTO_DOCKER_NETWORK_MODE=host moto_server
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
The Docker images used by Moto are taken from the `lambci/lambda`-repo by default. Use the following environment variable to configure a different repo:
|
2022-02-26 11:21:11 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
.. sourcecode:: bash
|
2022-02-26 11:21:11 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
MOTO_DOCKER_LAMBDA_IMAGE=mLupin/docker-lambda
|
2022-01-27 12:04:03 +00:00
|
|
|
|
2022-03-10 14:39:59 +00:00
|
|
|
.. note:: When using the decorators, a Docker container cannot reach Moto, as it does not run as a server. Any boto3-invocations used within your Lambda will try to connect to AWS.
|
2022-01-27 12:04:03 +00:00
|
|
|
"""
|
|
|
|
|
2022-06-04 11:30:16 +00:00
|
|
|
def __init__(self, region_name, account_id):
|
|
|
|
super().__init__(region_name, account_id)
|
2022-03-17 12:32:31 +00:00
|
|
|
self._lambdas = LambdaStorage(region_name=region_name)
|
2019-08-21 01:54:57 +00:00
|
|
|
self._event_source_mappings = {}
|
2021-01-17 15:28:49 +00:00
|
|
|
self._layers = LayerStorage()
|
2017-02-17 03:51:04 +00:00
|
|
|
|
2021-09-24 16:01:09 +00:00
|
|
|
@staticmethod
|
|
|
|
def default_vpc_endpoint_service(service_region, zones):
|
|
|
|
"""Default VPC endpoint service."""
|
|
|
|
return BaseBackend.default_vpc_endpoint_service_factory(
|
|
|
|
service_region, zones, "lambda"
|
|
|
|
)
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def create_alias(
|
|
|
|
self, name, function_name, function_version, description, routing_config
|
|
|
|
):
|
|
|
|
return self._lambdas.put_alias(
|
|
|
|
name, function_name, function_version, description, routing_config
|
|
|
|
)
|
|
|
|
|
|
|
|
def delete_alias(self, name, function_name):
|
|
|
|
return self._lambdas.delete_alias(name, function_name)
|
|
|
|
|
|
|
|
def get_alias(self, name, function_name):
|
|
|
|
return self._lambdas.get_alias(name, function_name)
|
|
|
|
|
|
|
|
def update_alias(
|
|
|
|
self, name, function_name, function_version, description, routing_config
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
The RevisionId parameter is not yet implemented
|
|
|
|
"""
|
|
|
|
return self._lambdas.update_alias(
|
|
|
|
name, function_name, function_version, description, routing_config
|
|
|
|
)
|
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
def create_function(self, spec):
|
2019-10-31 15:44:26 +00:00
|
|
|
function_name = spec.get("FunctionName", None)
|
2017-11-26 21:28:28 +00:00
|
|
|
if function_name is None:
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError("InvalidParameterValueException", "Missing FunctionName")
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
fn = LambdaFunction(spec, self.region_name, version="$LATEST")
|
2017-11-26 21:28:28 +00:00
|
|
|
|
|
|
|
self._lambdas.put_function(fn)
|
2017-09-13 19:00:39 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
if spec.get("Publish"):
|
2019-05-21 16:49:56 +00:00
|
|
|
ver = self.publish_function(function_name)
|
2021-08-28 14:26:44 +00:00
|
|
|
fn = copy.deepcopy(
|
|
|
|
fn
|
|
|
|
) # We don't want to change the actual version - just the return value
|
2019-05-21 16:49:56 +00:00
|
|
|
fn.version = ver.version
|
2016-02-12 19:39:20 +00:00
|
|
|
return fn
|
|
|
|
|
2019-08-21 01:54:57 +00:00
|
|
|
def create_event_source_mapping(self, spec):
|
2019-10-31 15:44:26 +00:00
|
|
|
required = ["EventSourceArn", "FunctionName"]
|
2019-08-21 01:54:57 +00:00
|
|
|
for param in required:
|
|
|
|
if not spec.get(param):
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError(
|
|
|
|
"InvalidParameterValueException", "Missing {}".format(param)
|
|
|
|
)
|
2019-08-21 01:54:57 +00:00
|
|
|
|
|
|
|
# Validate function name
|
2020-06-14 15:03:00 +00:00
|
|
|
func = self._lambdas.get_function_by_name_or_arn(spec.get("FunctionName", ""))
|
2019-08-21 01:54:57 +00:00
|
|
|
if not func:
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError("ResourceNotFoundException", "Invalid FunctionName")
|
2019-08-21 01:54:57 +00:00
|
|
|
|
|
|
|
# Validate queue
|
|
|
|
for queue in sqs_backends[self.region_name].queues.values():
|
2019-10-31 15:44:26 +00:00
|
|
|
if queue.queue_arn == spec["EventSourceArn"]:
|
|
|
|
if queue.lambda_event_source_mappings.get("func.function_arn"):
|
2019-08-21 01:54:57 +00:00
|
|
|
# TODO: Correct exception?
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError(
|
|
|
|
"ResourceConflictException", "The resource already exists."
|
|
|
|
)
|
2019-08-21 01:54:57 +00:00
|
|
|
if queue.fifo_queue:
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError(
|
|
|
|
"InvalidParameterValueException",
|
|
|
|
"{} is FIFO".format(queue.queue_arn),
|
|
|
|
)
|
2019-08-21 01:54:57 +00:00
|
|
|
else:
|
2019-10-31 15:44:26 +00:00
|
|
|
spec.update({"FunctionArn": func.function_arn})
|
2019-08-22 23:28:11 +00:00
|
|
|
esm = EventSourceMapping(spec)
|
2019-08-21 01:54:57 +00:00
|
|
|
self._event_source_mappings[esm.uuid] = esm
|
|
|
|
|
|
|
|
# Set backend function on queue
|
|
|
|
queue.lambda_event_source_mappings[esm.function_arn] = esm
|
|
|
|
|
|
|
|
return esm
|
2019-10-31 15:44:26 +00:00
|
|
|
for stream in json.loads(
|
|
|
|
dynamodbstreams_backends[self.region_name].list_streams()
|
|
|
|
)["Streams"]:
|
|
|
|
if stream["StreamArn"] == spec["EventSourceArn"]:
|
|
|
|
spec.update({"FunctionArn": func.function_arn})
|
2019-10-08 13:11:21 +00:00
|
|
|
esm = EventSourceMapping(spec)
|
|
|
|
self._event_source_mappings[esm.uuid] = esm
|
2019-10-31 15:44:26 +00:00
|
|
|
table_name = stream["TableName"]
|
2022-03-09 17:57:25 +00:00
|
|
|
table = dynamodb_backends[self.region_name].get_table(table_name)
|
2019-10-08 13:11:21 +00:00
|
|
|
table.lambda_event_source_mappings[esm.function_arn] = esm
|
|
|
|
return esm
|
2019-10-31 15:44:26 +00:00
|
|
|
raise RESTError("ResourceNotFoundException", "Invalid EventSourceArn")
|
2019-08-21 01:54:57 +00:00
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def publish_layer_version(self, spec):
|
|
|
|
required = ["LayerName", "Content"]
|
|
|
|
for param in required:
|
|
|
|
if not spec.get(param):
|
2022-03-17 12:32:31 +00:00
|
|
|
raise InvalidParameterValueException("Missing {}".format(param))
|
2021-01-17 15:28:49 +00:00
|
|
|
layer_version = LayerVersion(spec, self.region_name)
|
|
|
|
self._layers.put_layer_version(layer_version)
|
|
|
|
return layer_version
|
|
|
|
|
|
|
|
def list_layers(self):
|
|
|
|
return self._layers.list_layers()
|
|
|
|
|
2022-03-17 12:32:31 +00:00
|
|
|
def delete_layer_version(self, layer_name, layer_version):
|
|
|
|
return self._layers.delete_layer_version(layer_name, layer_version)
|
|
|
|
|
|
|
|
def get_layer_version(self, layer_name, layer_version):
|
|
|
|
return self._layers.get_layer_version(layer_name, layer_version)
|
|
|
|
|
2021-01-17 15:28:49 +00:00
|
|
|
def get_layer_versions(self, layer_name):
|
|
|
|
return self._layers.get_layer_versions(layer_name)
|
|
|
|
|
|
|
|
def layers_versions_by_arn(self, layer_version_arn):
|
|
|
|
return self._layers.get_layer_version_by_arn(layer_version_arn)
|
|
|
|
|
2021-08-28 14:26:44 +00:00
|
|
|
def publish_function(self, function_name, description=""):
|
|
|
|
return self._lambdas.publish_function(function_name, description)
|
2017-11-26 21:28:28 +00:00
|
|
|
|
2019-11-04 13:44:01 +00:00
|
|
|
def get_function(self, function_name_or_arn, qualifier=None):
|
2019-11-04 15:22:03 +00:00
|
|
|
return self._lambdas.get_function_by_name_or_arn(
|
|
|
|
function_name_or_arn, qualifier
|
|
|
|
)
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2019-02-16 15:27:23 +00:00
|
|
|
def list_versions_by_function(self, function_name):
|
|
|
|
return self._lambdas.list_versions_by_function(function_name)
|
|
|
|
|
2019-08-21 01:54:57 +00:00
|
|
|
def get_event_source_mapping(self, uuid):
|
|
|
|
return self._event_source_mappings.get(uuid)
|
|
|
|
|
|
|
|
def delete_event_source_mapping(self, uuid):
|
|
|
|
return self._event_source_mappings.pop(uuid)
|
|
|
|
|
|
|
|
def update_event_source_mapping(self, uuid, spec):
|
|
|
|
esm = self.get_event_source_mapping(uuid)
|
2020-06-14 15:03:00 +00:00
|
|
|
if not esm:
|
|
|
|
return False
|
|
|
|
|
2021-12-01 23:06:58 +00:00
|
|
|
for key in spec.keys():
|
2020-06-14 15:03:00 +00:00
|
|
|
if key == "FunctionName":
|
|
|
|
func = self._lambdas.get_function_by_name_or_arn(spec[key])
|
2019-08-21 01:54:57 +00:00
|
|
|
esm.function_arn = func.function_arn
|
2020-06-14 15:03:00 +00:00
|
|
|
elif key == "BatchSize":
|
|
|
|
esm.batch_size = spec[key]
|
|
|
|
elif key == "Enabled":
|
|
|
|
esm.enabled = spec[key]
|
|
|
|
|
|
|
|
esm.last_modified = time.mktime(datetime.datetime.utcnow().timetuple())
|
|
|
|
return esm
|
2019-08-21 01:54:57 +00:00
|
|
|
|
|
|
|
def list_event_source_mappings(self, event_source_arn, function_name):
|
|
|
|
esms = list(self._event_source_mappings.values())
|
|
|
|
if event_source_arn:
|
|
|
|
esms = list(filter(lambda x: x.event_source_arn == event_source_arn, esms))
|
|
|
|
if function_name:
|
|
|
|
esms = list(filter(lambda x: x.function_name == function_name, esms))
|
|
|
|
return esms
|
|
|
|
|
2017-09-13 19:00:39 +00:00
|
|
|
def get_function_by_arn(self, function_arn):
|
2017-11-26 21:28:28 +00:00
|
|
|
return self._lambdas.get_arn(function_arn)
|
2017-09-13 19:00:39 +00:00
|
|
|
|
2017-11-26 21:28:28 +00:00
|
|
|
def delete_function(self, function_name, qualifier=None):
|
2022-03-19 13:00:39 +00:00
|
|
|
self._lambdas.del_function(function_name, qualifier)
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2021-08-28 14:26:44 +00:00
|
|
|
def list_functions(self, func_version=None):
|
|
|
|
if func_version == "ALL":
|
|
|
|
return self._lambdas.all()
|
|
|
|
return self._lambdas.latest()
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2019-08-21 01:54:57 +00:00
|
|
|
def send_sqs_batch(self, function_arn, messages, queue_arn):
|
|
|
|
success = True
|
|
|
|
for message in messages:
|
|
|
|
func = self.get_function_by_arn(function_arn)
|
|
|
|
result = self._send_sqs_message(func, message, queue_arn)
|
|
|
|
if not result:
|
|
|
|
success = False
|
|
|
|
return success
|
|
|
|
|
|
|
|
def _send_sqs_message(self, func, message, queue_arn):
|
|
|
|
event = {
|
|
|
|
"Records": [
|
|
|
|
{
|
|
|
|
"messageId": message.id,
|
|
|
|
"receiptHandle": message.receipt_handle,
|
|
|
|
"body": message.body,
|
|
|
|
"attributes": {
|
|
|
|
"ApproximateReceiveCount": "1",
|
|
|
|
"SentTimestamp": "1545082649183",
|
|
|
|
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
|
2019-10-31 15:44:26 +00:00
|
|
|
"ApproximateFirstReceiveTimestamp": "1545082649185",
|
2019-08-21 01:54:57 +00:00
|
|
|
},
|
|
|
|
"messageAttributes": {},
|
|
|
|
"md5OfBody": "098f6bcd4621d373cade4e832627b4f6",
|
|
|
|
"eventSource": "aws:sqs",
|
|
|
|
"eventSourceARN": queue_arn,
|
2019-10-31 15:44:26 +00:00
|
|
|
"awsRegion": self.region_name,
|
2019-08-21 01:54:57 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
request_headers = {}
|
|
|
|
response_headers = {}
|
|
|
|
func.invoke(json.dumps(event), request_headers, response_headers)
|
2019-10-31 15:44:26 +00:00
|
|
|
return "x-amz-function-error" not in response_headers
|
2019-08-21 01:54:57 +00:00
|
|
|
|
|
|
|
def send_sns_message(self, function_name, message, subject=None, qualifier=None):
|
2017-09-27 23:04:58 +00:00
|
|
|
event = {
|
|
|
|
"Records": [
|
|
|
|
{
|
|
|
|
"EventVersion": "1.0",
|
|
|
|
"EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
|
|
|
|
"EventSource": "aws:sns",
|
|
|
|
"Sns": {
|
|
|
|
"SignatureVersion": "1",
|
|
|
|
"Timestamp": "1970-01-01T00:00:00.000Z",
|
|
|
|
"Signature": "EXAMPLE",
|
|
|
|
"SigningCertUrl": "EXAMPLE",
|
|
|
|
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
|
|
|
|
"Message": message,
|
|
|
|
"MessageAttributes": {
|
2019-10-31 15:44:26 +00:00
|
|
|
"Test": {"Type": "String", "Value": "TestString"},
|
|
|
|
"TestBinary": {"Type": "Binary", "Value": "TestBinary"},
|
2017-09-27 23:04:58 +00:00
|
|
|
},
|
|
|
|
"Type": "Notification",
|
|
|
|
"UnsubscribeUrl": "EXAMPLE",
|
|
|
|
"TopicArn": "arn:aws:sns:EXAMPLE",
|
2019-10-31 15:44:26 +00:00
|
|
|
"Subject": subject or "TestInvoke",
|
|
|
|
},
|
2017-09-27 23:04:58 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-11-04 13:44:01 +00:00
|
|
|
func = self._lambdas.get_function_by_name_or_arn(function_name, qualifier)
|
2018-03-22 05:14:10 +00:00
|
|
|
func.invoke(json.dumps(event), {}, {})
|
2017-09-27 23:04:58 +00:00
|
|
|
|
2019-10-07 10:11:22 +00:00
|
|
|
def send_dynamodb_items(self, function_arn, items, source):
|
2019-10-31 15:44:26 +00:00
|
|
|
event = {
|
|
|
|
"Records": [
|
|
|
|
{
|
|
|
|
"eventID": item.to_json()["eventID"],
|
|
|
|
"eventName": "INSERT",
|
|
|
|
"eventVersion": item.to_json()["eventVersion"],
|
|
|
|
"eventSource": item.to_json()["eventSource"],
|
|
|
|
"awsRegion": self.region_name,
|
|
|
|
"dynamodb": item.to_json()["dynamodb"],
|
|
|
|
"eventSourceARN": source,
|
|
|
|
}
|
|
|
|
for item in items
|
|
|
|
]
|
|
|
|
}
|
2019-10-07 10:11:22 +00:00
|
|
|
func = self._lambdas.get_arn(function_arn)
|
2020-02-24 09:28:52 +00:00
|
|
|
return func.invoke(json.dumps(event), {}, {})
|
2019-10-07 10:11:22 +00:00
|
|
|
|
2020-05-12 12:34:10 +00:00
|
|
|
def send_log_event(
|
|
|
|
self, function_arn, filter_name, log_group_name, log_stream_name, log_events
|
|
|
|
):
|
|
|
|
data = {
|
|
|
|
"messageType": "DATA_MESSAGE",
|
2022-05-08 22:25:40 +00:00
|
|
|
"owner": get_account_id(),
|
2020-05-12 12:34:10 +00:00
|
|
|
"logGroup": log_group_name,
|
|
|
|
"logStream": log_stream_name,
|
|
|
|
"subscriptionFilters": [filter_name],
|
|
|
|
"logEvents": log_events,
|
|
|
|
}
|
|
|
|
|
|
|
|
output = io.BytesIO()
|
|
|
|
with GzipFile(fileobj=output, mode="w") as f:
|
|
|
|
f.write(json.dumps(data, separators=(",", ":")).encode("utf-8"))
|
|
|
|
payload_gz_encoded = base64.b64encode(output.getvalue()).decode("utf-8")
|
|
|
|
|
|
|
|
event = {"awslogs": {"data": payload_gz_encoded}}
|
|
|
|
|
|
|
|
func = self._lambdas.get_arn(function_arn)
|
|
|
|
return func.invoke(json.dumps(event), {}, {})
|
|
|
|
|
2017-09-13 19:00:39 +00:00
|
|
|
def list_tags(self, resource):
|
2022-03-19 13:00:39 +00:00
|
|
|
return self._lambdas.get_function_by_name_or_arn(resource).tags
|
2017-09-13 19:00:39 +00:00
|
|
|
|
|
|
|
def tag_resource(self, resource, tags):
|
2022-03-19 13:00:39 +00:00
|
|
|
fn = self._lambdas.get_function_by_name_or_arn(resource)
|
2017-11-26 21:28:28 +00:00
|
|
|
fn.tags.update(tags)
|
2017-09-13 19:00:39 +00:00
|
|
|
|
|
|
|
def untag_resource(self, resource, tagKeys):
|
2022-03-19 13:00:39 +00:00
|
|
|
fn = self._lambdas.get_function_by_name_or_arn(resource)
|
|
|
|
for key in tagKeys:
|
|
|
|
fn.tags.pop(key, None)
|
|
|
|
|
|
|
|
def add_permission(self, function_name, qualifier, raw):
|
|
|
|
fn = self.get_function(function_name, qualifier)
|
|
|
|
fn.policy.add_statement(raw, qualifier)
|
2020-01-23 18:46:24 +00:00
|
|
|
|
2020-04-03 09:30:05 +00:00
|
|
|
def remove_permission(self, function_name, sid, revision=""):
|
2020-01-23 18:46:24 +00:00
|
|
|
fn = self.get_function(function_name)
|
|
|
|
fn.policy.del_statement(sid, revision)
|
|
|
|
|
2022-02-08 21:12:51 +00:00
|
|
|
def get_code_signing_config(self, function_name):
|
|
|
|
fn = self.get_function(function_name)
|
|
|
|
return fn.get_code_signing_config()
|
|
|
|
|
2020-01-23 18:46:24 +00:00
|
|
|
def get_policy(self, function_name):
|
|
|
|
fn = self.get_function(function_name)
|
2022-03-17 12:32:31 +00:00
|
|
|
if not fn:
|
|
|
|
raise UnknownFunctionException(function_name)
|
2020-01-23 18:46:24 +00:00
|
|
|
return fn.policy.wire_format()
|
2017-10-03 02:23:00 +00:00
|
|
|
|
2019-11-15 16:34:14 +00:00
|
|
|
def update_function_code(self, function_name, qualifier, body):
|
|
|
|
fn = self.get_function(function_name, qualifier)
|
|
|
|
|
|
|
|
if fn:
|
|
|
|
if body.get("Publish", False):
|
|
|
|
fn = self.publish_function(function_name)
|
|
|
|
|
|
|
|
config = fn.update_function_code(body)
|
|
|
|
return config
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def update_function_configuration(self, function_name, qualifier, body):
|
|
|
|
fn = self.get_function(function_name, qualifier)
|
|
|
|
|
|
|
|
return fn.update_configuration(body) if fn else None
|
|
|
|
|
|
|
|
def invoke(self, function_name, qualifier, body, headers, response_headers):
|
|
|
|
fn = self.get_function(function_name, qualifier)
|
|
|
|
if fn:
|
|
|
|
payload = fn.invoke(body, headers, response_headers)
|
|
|
|
response_headers["Content-Length"] = str(len(payload))
|
2020-05-07 09:55:15 +00:00
|
|
|
return payload
|
2019-11-15 16:34:14 +00:00
|
|
|
else:
|
2020-05-07 09:55:15 +00:00
|
|
|
return None
|
2019-11-15 16:34:14 +00:00
|
|
|
|
2020-08-26 10:06:53 +00:00
|
|
|
def put_function_concurrency(self, function_name, reserved_concurrency):
|
|
|
|
fn = self.get_function(function_name)
|
|
|
|
fn.reserved_concurrency = reserved_concurrency
|
|
|
|
return fn.reserved_concurrency
|
|
|
|
|
|
|
|
def delete_function_concurrency(self, function_name):
|
|
|
|
fn = self.get_function(function_name)
|
|
|
|
fn.reserved_concurrency = None
|
|
|
|
return fn.reserved_concurrency
|
|
|
|
|
|
|
|
def get_function_concurrency(self, function_name):
|
|
|
|
fn = self.get_function(function_name)
|
|
|
|
return fn.reserved_concurrency
|
|
|
|
|
2016-02-12 19:39:20 +00:00
|
|
|
|
2017-05-24 12:54:00 +00:00
|
|
|
def do_validate_s3():
|
2019-10-31 15:44:26 +00:00
|
|
|
return os.environ.get("VALIDATE_LAMBDA_S3", "") in ["", "1", "true"]
|
2017-05-24 12:54:00 +00:00
|
|
|
|
|
|
|
|
2021-12-24 21:02:45 +00:00
|
|
|
lambda_backends = BackendDict(LambdaBackend, "lambda")
|