1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
import json
import os
import sys
from unittest import SkipTest, mock
from uuid import uuid4
import boto3
import pytest
from botocore.exceptions import ClientError
from moto import mock_aws, settings
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from moto.utilities.distutils_version import LooseVersion
from tests.test_awslambda import delete_all_layer_versions
from tests.test_s3 import s3_aws_verified
from .utilities import get_role_name, get_test_zip_file1
PYTHON_VERSION = "python3.11"
_lambda_region = "us-west-2"
boto3_version = sys.modules["botocore"].__version__
@mock_aws
def test_publish_lambda_layers__without_content():
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
with pytest.raises(ClientError) as exc:
conn.publish_layer_version(
LayerName=layer_name,
Content={},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
)
err = exc.value.response["Error"]
assert err["Code"] == "InvalidParameterValueException"
assert err["Message"] == "Missing Content"
@mock_aws
@mock.patch.dict(os.environ, {"VALIDATE_LAMBDA_S3": "false"})
def test_publish_layer_with_unknown_s3_file():
if not settings.TEST_DECORATOR_MODE:
raise SkipTest("Can only set env var in DecoratorMode")
conn = boto3.client("lambda", _lambda_region)
content = conn.publish_layer_version(
LayerName=str(uuid4())[0:6],
Content={"S3Bucket": "my-bucket", "S3Key": "my-key.zip"},
)["Content"]
assert content["CodeSha256"] == ""
assert content["CodeSize"] == 0
@s3_aws_verified
def test_list_lambda_layers(account_id, bucket_name=None):
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
s3_conn = boto3.client("s3", "us-east-1")
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", "us-east-1")
layer_name = str(uuid4())[0:6]
try:
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.13"],
LicenseInfo="MIT",
)
conn.publish_layer_version(
LayerName=layer_name,
Content={"S3Bucket": bucket_name, "S3Key": "test.zip"},
CompatibleRuntimes=["python3.13"],
LicenseInfo="MIT",
)
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.14"],
LicenseInfo="MIT",
)
result = conn.list_layer_versions(LayerName=layer_name)
for version in result["LayerVersions"]:
version.pop("CreatedDate")
expected_arn = f"arn:aws:lambda:us-east-1:{account_id}:layer:{layer_name}:"
assert result["LayerVersions"] == [
{
"Version": 3,
"LayerVersionArn": expected_arn + "3",
"CompatibleRuntimes": ["python3.14"],
"LicenseInfo": "MIT",
},
{
"Version": 2,
"LayerVersionArn": expected_arn + "2",
"CompatibleRuntimes": ["python3.13"],
"LicenseInfo": "MIT",
},
{
"Version": 1,
"LayerVersionArn": expected_arn + "1",
"CompatibleRuntimes": ["python3.13"],
"LicenseInfo": "MIT",
},
]
finally:
delete_all_layer_versions(conn, layer_name=layer_name)
@mock_aws
def test_create_function_with_layer():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.14"],
LicenseInfo="MIT",
)
conn.publish_layer_version(
LayerName=layer_name,
Content={"S3Bucket": bucket_name, "S3Key": "test.zip"},
CompatibleRuntimes=["python3.14"],
LicenseInfo="MIT",
)
expected_arn = f"arn:aws:lambda:{_lambda_region}:{ACCOUNT_ID}:layer:{layer_name}:"
function_name = str(uuid4())[0:6]
conn.create_function(
FunctionName=function_name,
Runtime=PYTHON_VERSION,
Role=get_role_name(),
Handler="lambda_function.lambda_handler",
Code={"S3Bucket": bucket_name, "S3Key": "test.zip"},
Layers=[(expected_arn + "1")],
)
result = conn.get_function_configuration(FunctionName=function_name)
assert result["Layers"] == [
{"Arn": (expected_arn + "1"), "CodeSize": len(zip_content)}
]
result = conn.update_function_configuration(
FunctionName=function_name, Layers=[(expected_arn + "2")]
)
assert result["Layers"] == [
{"Arn": (expected_arn + "2"), "CodeSize": len(zip_content)}
]
@mock_aws
def test_list_lambda_layers_with_unknown_name():
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
# Test get layer versions for nonexistent layer
result = conn.list_layer_versions(LayerName=layer_name)
assert result["LayerVersions"] == []
@mock_aws
def test_create_function_with_unknown_layer():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
unknown_arn = f"arn:aws:lambda:{_lambda_region}:{ACCOUNT_ID}:layer:{layer_name}:1"
# Test create function with nonexistent layer version
function_name = str(uuid4())[0:6]
with pytest.raises(ClientError) as exc:
conn.create_function(
FunctionName=function_name,
Runtime=PYTHON_VERSION,
Role=get_role_name(),
Handler="lambda_function.lambda_handler",
Code={"S3Bucket": bucket_name, "S3Key": "test.zip"},
Layers=[unknown_arn],
)
err = exc.value.response["Error"]
assert err["Code"] == "ResourceNotFoundException"
assert (
err["Message"] == f"One or more LayerVersion does not exist ['{unknown_arn}']"
)
@mock_aws
def test_get_layer_version():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
resp = conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
CompatibleArchitectures=["x86_64"],
)
layer_version = resp["Version"]
resp = conn.get_layer_version(LayerName=layer_name, VersionNumber=layer_version)
assert resp["Version"] == 1
assert resp["CompatibleArchitectures"] == ["x86_64"]
assert resp["CompatibleRuntimes"] == ["python3.6"]
assert resp["LicenseInfo"] == "MIT"
@mock_aws
def test_get_layer_version__unknown():
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
# Delete Layer that never existed
with pytest.raises(ClientError) as exc:
conn.get_layer_version(LayerName=layer_name, VersionNumber=1)
err = exc.value.response["Error"]
assert err["Code"] == "ResourceNotFoundException"
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
)
# Delete Version that never existed
with pytest.raises(ClientError) as exc:
conn.get_layer_version(LayerName=layer_name, VersionNumber=999)
err = exc.value.response["Error"]
assert err["Code"] == "ResourceNotFoundException"
@mock_aws
@pytest.mark.parametrize("use_arn", [True, False])
def test_delete_layer_version(use_arn):
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
resp = conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
)
layer_arn = resp["LayerArn"]
layer_version = resp["Version"]
if use_arn:
conn.get_layer_version(LayerName=layer_arn, VersionNumber=layer_version)
conn.delete_layer_version(LayerName=layer_arn, VersionNumber=layer_version)
else:
conn.get_layer_version(LayerName=layer_name, VersionNumber=layer_version)
conn.delete_layer_version(LayerName=layer_name, VersionNumber=layer_version)
result = conn.list_layer_versions(LayerName=layer_name)["LayerVersions"]
assert result == []
@mock_aws
def test_get_layer_with_no_layer_versions():
def get_layer_by_layer_name_from_list_of_layer_dicts(layer_name, layer_list):
for layer in layer_list:
if layer["LayerName"] == layer_name:
return layer
return None
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
# Publish a new Layer and assert Layer exists and only version 1 is there
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
)
assert (
get_layer_by_layer_name_from_list_of_layer_dicts(
layer_name, conn.list_layers()["Layers"]
)["LatestMatchingVersion"]["Version"]
== 1
)
# Add a new version of that Layer then delete that version
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
)
assert (
get_layer_by_layer_name_from_list_of_layer_dicts(
layer_name, conn.list_layers()["Layers"]
)["LatestMatchingVersion"]["Version"]
== 2
)
conn.delete_layer_version(LayerName=layer_name, VersionNumber=2)
assert (
get_layer_by_layer_name_from_list_of_layer_dicts(
layer_name, conn.list_layers()["Layers"]
)["LatestMatchingVersion"]["Version"]
== 1
)
# Delete the last layer_version and check that the Layer is still in the LayerStorage
conn.delete_layer_version(LayerName=layer_name, VersionNumber=1)
assert (
get_layer_by_layer_name_from_list_of_layer_dicts(
layer_name, conn.list_layers()["Layers"]
)
is None
)
# Assert _latest_version didn't decrement
conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
)
assert (
get_layer_by_layer_name_from_list_of_layer_dicts(
layer_name, conn.list_layers()["Layers"]
)["LatestMatchingVersion"]["Version"]
== 3
)
@mock_aws
def test_add_layer_version_permission():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
resp = conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
CompatibleArchitectures=["x86_64"],
)
layer_version = resp["Version"]
resp = conn.add_layer_version_permission(
LayerName=layer_name,
VersionNumber=layer_version,
StatementId="xaccount",
Action="lambda:GetLayerVersion",
Principal="432143214321",
OrganizationId="o-123456",
)
assert "RevisionId" in resp
assert "Statement" in resp
res = json.loads(resp["Statement"])
assert res["Action"] == "lambda:GetLayerVersion"
@mock_aws
def test_get_layer_version_policy():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
resp = conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
CompatibleArchitectures=["x86_64"],
)
layer_version = resp["Version"]
conn.add_layer_version_permission(
LayerName=layer_name,
VersionNumber=layer_version,
StatementId="xaccount",
Action="lambda:GetLayerVersion",
Principal="432143214321",
)
resp = conn.get_layer_version_policy(
LayerName=layer_name, VersionNumber=layer_version
)
assert "Policy" in resp
assert "RevisionId" in resp
res = json.loads(resp["Policy"])
assert res["Statement"][0]["Action"] == "lambda:GetLayerVersion"
assert (
res["Statement"][0]["Resource"]
== f"arn:aws:lambda:us-west-2:123456789012:layer:{layer_name}:1"
)
@mock_aws
def test_remove_layer_version_permission():
if LooseVersion(boto3_version) < LooseVersion("1.29.0"):
raise SkipTest("Parameters only available in newer versions")
bucket_name = str(uuid4())
s3_conn = boto3.client("s3", _lambda_region)
s3_conn.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={"LocationConstraint": _lambda_region},
)
zip_content = get_test_zip_file1()
s3_conn.put_object(Bucket=bucket_name, Key="test.zip", Body=zip_content)
conn = boto3.client("lambda", _lambda_region)
layer_name = str(uuid4())[0:6]
resp = conn.publish_layer_version(
LayerName=layer_name,
Content={"ZipFile": get_test_zip_file1()},
CompatibleRuntimes=["python3.6"],
LicenseInfo="MIT",
CompatibleArchitectures=["x86_64"],
)
layer_version = resp["Version"]
conn.add_layer_version_permission(
LayerName=layer_name,
VersionNumber=layer_version,
StatementId="xaccount",
Action="lambda:GetLayerVersion",
Principal="432143214321",
)
resp = conn.get_layer_version_policy(
LayerName=layer_name, VersionNumber=layer_version
)
assert "Policy" in resp
resp = conn.remove_layer_version_permission(
LayerName=layer_name,
VersionNumber=layer_version,
StatementId="xaccount",
)
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 204
with pytest.raises(ClientError) as exc:
conn.get_layer_version_policy(
LayerName=layer_name, VersionNumber=layer_version
)["Policy"]
err = exc.value.response["Error"]
assert err["Code"] == "ResourceNotFoundException"
assert err["Message"] == "The resource you requested does not exist."
|