2022-03-17 12:32:31 +00:00
import base64
2023-11-30 15:55:51 +00:00
import hashlib
2022-11-23 13:16:33 +00:00
import json
import os
2024-01-17 10:02:15 +00:00
import sys
2023-06-28 21:23:33 +00:00
from unittest import SkipTest , mock
2023-11-30 15:55:51 +00:00
from uuid import uuid4
2016-02-12 19:39:20 +00:00
import boto3
2021-09-21 15:19:49 +00:00
import pytest
2023-07-06 16:45:29 +00:00
from botocore . exceptions import ClientError , ParamValidationError
2016-02-12 19:39:20 +00:00
from freezegun import freeze_time
2023-11-30 15:55:51 +00:00
2024-01-07 12:03:33 +00:00
from moto import mock_aws , settings
2022-08-13 09:49:43 +00:00
from moto . core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2024-01-17 10:02:15 +00:00
from moto . utilities . distutils_version import LooseVersion
2023-11-30 15:55:51 +00:00
from tests . test_ecr . test_ecr_helpers import _create_image_manifest
2023-12-10 20:31:33 +00:00
from . import lambda_aws_verified
2021-09-21 15:19:49 +00:00
from . utilities import (
2023-11-30 15:55:51 +00:00
_process_lambda ,
create_invalid_lambda ,
2021-09-21 15:19:49 +00:00
get_role_name ,
get_test_zip_file1 ,
get_test_zip_file2 ,
2022-12-05 10:31:20 +00:00
get_test_zip_file3 ,
2019-10-31 15:44:26 +00:00
)
2016-02-16 21:43:33 +00:00
2023-08-31 06:47:49 +00:00
PYTHON_VERSION = " python3.11 "
2023-12-01 23:07:52 +00:00
LAMBDA_FUNC_NAME = " test "
2017-11-26 21:28:28 +00:00
_lambda_region = " us-west-2 "
2016-02-16 21:43:33 +00:00
2024-01-17 10:02:15 +00:00
boto3_version = sys . modules [ " botocore " ] . __version__
2017-09-27 23:04:58 +00:00
2023-06-28 21:23:33 +00:00
@mock.patch.dict ( " os.environ " , { " MOTO_ENABLE_ISO_REGIONS " : " true " } )
2023-06-18 12:05:55 +00:00
@pytest.mark.parametrize ( " region " , [ " us-west-2 " , " cn-northwest-1 " , " us-isob-east-1 " ] )
2024-01-07 12:03:33 +00:00
@mock_aws
2021-01-08 14:22:12 +00:00
def test_lambda_regions ( region ) :
2023-09-27 18:34:30 +00:00
if not settings . TEST_DECORATOR_MODE :
raise SkipTest ( " Can only set EnvironVars in DecoratorMode " )
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " ISO-region not available in older versions " )
2021-01-08 14:22:12 +00:00
client = boto3 . client ( " lambda " , region_name = region )
resp = client . list_functions ( )
2023-06-11 18:44:30 +00:00
assert resp [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
2021-01-08 14:22:12 +00:00
2023-12-10 20:31:33 +00:00
@pytest.mark.aws_verified
@lambda_aws_verified
def test_list_functions ( iam_role_arn = None ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-12-10 20:31:33 +00:00
sts = boto3 . client ( " sts " , " eu-west-2 " )
account_id = sts . get_caller_identity ( ) [ " Account " ]
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2023-12-10 20:31:33 +00:00
function_name = " moto_test_ " + str ( uuid4 ( ) ) [ 0 : 6 ]
2021-09-21 15:19:49 +00:00
initial_list = conn . list_functions ( ) [ " Functions " ]
initial_names = [ f [ " FunctionName " ] for f in initial_list ]
2023-06-11 18:44:30 +00:00
assert function_name not in initial_names
2021-08-28 14:26:44 +00:00
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-12-10 20:31:33 +00:00
Role = iam_role_arn ,
2021-08-28 14:26:44 +00:00
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : get_test_zip_file1 ( ) } ,
)
2023-12-10 20:31:33 +00:00
wait_for_func = conn . get_waiter ( " function_active " )
wait_for_func . wait ( FunctionName = function_name , WaiterConfig = { " Delay " : 1 } )
2021-09-21 15:19:49 +00:00
names = [ f [ " FunctionName " ] for f in conn . list_functions ( ) [ " Functions " ] ]
2023-06-11 18:44:30 +00:00
assert function_name in names
2021-09-21 15:19:49 +00:00
2021-08-28 14:26:44 +00:00
conn . publish_version ( FunctionName = function_name , Description = " v2 " )
2021-09-21 15:19:49 +00:00
func_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 1
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) > LooseVersion ( " 1.29.0 " ) :
# PackageType only available in new versions
assert our_functions [ 0 ] [ " PackageType " ] == " Zip "
2021-08-28 14:26:44 +00:00
# FunctionVersion=ALL means we should get a list of all versions
2021-09-21 15:19:49 +00:00
full_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in full_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 2
2023-12-10 20:31:33 +00:00
for func in our_functions :
assert func [ " PackageType " ] == " Zip "
2021-08-28 14:26:44 +00:00
2021-09-21 15:19:49 +00:00
v1 = [ f for f in our_functions if f [ " Version " ] == " 1 " ] [ 0 ]
2023-06-11 18:44:30 +00:00
assert v1 [ " Description " ] == " v2 "
assert (
v1 [ " FunctionArn " ]
2023-12-10 20:31:33 +00:00
== f " arn:aws:lambda: { _lambda_region } : { account_id } :function: { function_name } :1 "
2021-08-28 14:26:44 +00:00
)
2021-09-21 15:19:49 +00:00
latest = [ f for f in our_functions if f [ " Version " ] == " $LATEST " ] [ 0 ]
2023-06-11 18:44:30 +00:00
assert latest [ " Description " ] == " "
assert (
latest [ " FunctionArn " ]
2023-12-10 20:31:33 +00:00
== f " arn:aws:lambda: { _lambda_region } : { account_id } :function: { function_name } :$LATEST "
2021-08-28 14:26:44 +00:00
)
2016-02-12 19:39:20 +00:00
2023-12-10 20:31:33 +00:00
conn . delete_function ( FunctionName = function_name )
2016-12-03 23:17:15 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-16 21:43:33 +00:00
def test_create_based_on_s3_with_missing_bucket ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-16 21:43:33 +00:00
2023-06-11 18:44:30 +00:00
with pytest . raises ( ClientError ) as exc :
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-06-11 18:44:30 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : " this-bucket-does-not-exist " , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
VpcConfig = {
" SecurityGroupIds " : [ " sg-123abc " ] ,
" SubnetIds " : [ " subnet-123abc " ] ,
} ,
)
err = exc . value . response [ " Error " ]
assert (
err [ " Message " ]
== " Error occurred while GetObject. S3 Error Code: NoSuchBucket. S3 Error Message: The specified bucket does not exist "
)
2016-02-16 21:43:33 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-16 21:43:33 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
2016-02-16 19:17:10 +00:00
def test_create_function_from_aws_bucket ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-10-06 09:52:23 +00:00
zip_content = get_test_zip_file2 ( )
2016-10-09 15:13:52 +00:00
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 19:39:20 +00:00
result = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 23:04:58 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 19:39:20 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
2022-02-08 21:12:51 +00:00
PackageType = " ZIP " ,
2016-02-12 19:39:20 +00:00
Publish = True ,
2016-02-16 20:35:29 +00:00
VpcConfig = { " SecurityGroupIds " : [ " sg-123abc " ] , " SubnetIds " : [ " subnet-123abc " ] } ,
2016-02-12 19:39:20 +00:00
)
2022-03-17 12:32:31 +00:00
2023-06-11 18:44:30 +00:00
assert result [ " FunctionName " ] == function_name
assert (
result [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2016-02-16 20:15:34 +00:00
)
2023-08-31 06:47:49 +00:00
assert result [ " Runtime " ] == PYTHON_VERSION
2023-06-11 18:44:30 +00:00
assert result [ " Handler " ] == " lambda_function.lambda_handler "
assert result [ " CodeSha256 " ] == base64 . b64encode (
hashlib . sha256 ( zip_content ) . digest ( )
) . decode ( " utf-8 " )
assert result [ " State " ] == " Active "
2016-02-16 20:15:34 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-16 20:15:34 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_create_function_from_zipfile ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2016-10-06 09:52:23 +00:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-16 20:15:34 +00:00
result = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 23:04:58 +00:00
Handler = " lambda_function.lambda_handler " ,
2016-02-16 20:15:34 +00:00
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2017-02-24 02:37:43 +00:00
# this is hard to match against, so remove it
result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2024-01-14 13:01:30 +00:00
result [ " ResponseMetadata " ] . pop ( " RequestId " )
2016-02-16 20:15:34 +00:00
result . pop ( " LastModified " )
2016-02-12 19:39:20 +00:00
2023-06-11 18:44:30 +00:00
assert result == {
2023-07-06 16:45:29 +00:00
" Architectures " : [ " x86_64 " ] ,
" EphemeralStorage " : { " Size " : 512 } ,
2023-06-11 18:44:30 +00:00
" FunctionName " : function_name ,
" FunctionArn " : f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } " ,
2023-08-31 06:47:49 +00:00
" Runtime " : PYTHON_VERSION ,
2023-06-11 18:44:30 +00:00
" Role " : result [ " Role " ] ,
" Handler " : " lambda_function.lambda_handler " ,
" CodeSize " : len ( zip_content ) ,
" Description " : " test lambda function " ,
" Timeout " : 3 ,
" MemorySize " : 128 ,
2023-12-10 20:31:33 +00:00
" PackageType " : " Zip " ,
2023-06-11 18:44:30 +00:00
" CodeSha256 " : base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode (
" utf-8 "
) ,
" Version " : " 1 " ,
" VpcConfig " : { " SecurityGroupIds " : [ ] , " SubnetIds " : [ ] } ,
" ResponseMetadata " : { " HTTPStatusCode " : 201 } ,
" State " : " Active " ,
" Layers " : [ ] ,
" TracingConfig " : { " Mode " : " PassThrough " } ,
2023-07-06 16:45:29 +00:00
" SnapStart " : { " ApplyOn " : " None " , " OptimizationStatus " : " Off " } ,
2023-06-11 18:44:30 +00:00
}
2016-02-12 19:39:20 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-07-06 16:45:29 +00:00
def test_create_function_from_image ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-07-06 16:45:29 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
image_config = {
" EntryPoint " : [
" python " ,
] ,
" Command " : [
" /opt/app.py " ,
] ,
" WorkingDirectory " : " /opt " ,
}
conn . create_function (
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
Description = " test lambda function " ,
ImageConfig = image_config ,
PackageType = " Image " ,
)
result = conn . get_function ( FunctionName = function_name )
assert " ImageConfigResponse " in result [ " Configuration " ]
assert result [ " Configuration " ] [ " ImageConfigResponse " ] [ " ImageConfig " ] == image_config
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-01 21:42:12 +00:00
def test_create_function_from_image_default_working_directory ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-08-01 21:42:12 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
image_config = {
" EntryPoint " : [
" python " ,
] ,
" Command " : [
" /opt/app.py " ,
] ,
}
conn . create_function (
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
Description = " test lambda function " ,
ImageConfig = image_config ,
PackageType = " Image " ,
)
result = conn . get_function ( FunctionName = function_name )
assert " ImageConfigResponse " in result [ " Configuration " ]
assert result [ " Configuration " ] [ " ImageConfigResponse " ] [ " ImageConfig " ] == image_config
2024-01-07 12:03:33 +00:00
@mock_aws
2023-07-06 16:45:29 +00:00
def test_create_function_error_bad_architecture ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-07-06 16:45:29 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
with pytest . raises ( ClientError ) as exc :
conn . create_function (
Architectures = [ " foo " ] ,
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
)
err = exc . value . response
assert err [ " Error " ] [ " Code " ] == " ValidationException "
assert (
err [ " Error " ] [ " Message " ]
== " 1 validation error detected: Value ' [ ' foo ' ] ' at ' architectures ' failed to satisfy "
" constraint: Member must satisfy constraint: [Member must satisfy enum value set: "
" [x86_64, arm64], Member must not be null] "
)
2024-01-07 12:03:33 +00:00
@mock_aws
2023-07-06 16:45:29 +00:00
def test_create_function_error_ephemeral_too_big ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-07-06 16:45:29 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
with pytest . raises ( ClientError ) as exc :
conn . create_function (
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
EphemeralStorage = { " Size " : 3000000 } ,
)
err = exc . value . response
assert err [ " Error " ] [ " Code " ] == " ValidationException "
assert (
err [ " Error " ] [ " Message " ]
== " 1 validation error detected: Value ' 3000000 ' at ' ephemeralStorage.size ' "
" failed to satisfy constraint: "
" Member must have value less than or equal to 10240 "
)
2024-01-07 12:03:33 +00:00
@mock_aws
2023-07-06 16:45:29 +00:00
def test_create_function_error_ephemeral_too_small ( ) :
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
with pytest . raises ( ParamValidationError ) as exc :
conn . create_function (
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
EphemeralStorage = { " Size " : 200 } ,
)
# this one is handled by botocore, not moto
assert exc . typename == " ParamValidationError "
2024-01-07 12:03:33 +00:00
@mock_aws
2022-03-17 12:32:31 +00:00
@pytest.mark.parametrize (
" tracing_mode " ,
[ ( None , " PassThrough " ) , ( " PassThrough " , " PassThrough " ) , ( " Active " , " Active " ) ] ,
)
def test_create_function__with_tracingmode ( tracing_mode ) :
conn = boto3 . client ( " lambda " , _lambda_region )
source , output = tracing_mode
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
kwargs = dict (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2022-03-17 12:32:31 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
if source :
kwargs [ " TracingConfig " ] = { " Mode " : source }
result = conn . create_function ( * * kwargs )
2023-06-11 18:44:30 +00:00
assert result [ " TracingConfig " ] == { " Mode " : output }
2022-03-17 12:32:31 +00:00
2022-11-23 13:16:33 +00:00
@pytest.fixture ( name = " with_ecr_mock " )
def ecr_repo_fixture ( ) :
2024-01-07 12:03:33 +00:00
with mock_aws ( ) :
2022-11-23 13:16:33 +00:00
os . environ [ " MOTO_LAMBDA_STUB_ECR " ] = " FALSE "
repo_name = " testlambdaecr "
2023-06-11 18:44:30 +00:00
ecr_client = boto3 . client ( " ecr " , " us-east-1 " )
2022-11-23 13:16:33 +00:00
ecr_client . create_repository ( repositoryName = repo_name )
2022-12-16 18:22:43 +00:00
response = ecr_client . put_image (
2022-11-23 13:16:33 +00:00
repositoryName = repo_name ,
imageManifest = json . dumps ( _create_image_manifest ( ) ) ,
imageTag = " latest " ,
)
2022-12-16 18:22:43 +00:00
yield response [ " image " ] [ " imageId " ]
2022-11-23 13:16:33 +00:00
ecr_client . delete_repository ( repositoryName = repo_name , force = True )
os . environ [ " MOTO_LAMBDA_STUB_ECR " ] = " TRUE "
2024-01-07 12:03:33 +00:00
@mock_aws
2022-11-23 13:16:33 +00:00
def test_create_function_from_stubbed_ecr ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2022-06-10 14:09:13 +00:00
lambda_client = boto3 . client ( " lambda " , " us-east-1 " )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = " 111122223333.dkr.ecr.us-east-1.amazonaws.com/testlambda:latest "
2022-11-23 13:16:33 +00:00
2022-06-10 14:09:13 +00:00
dic = {
" FunctionName " : fn_name ,
" Role " : get_role_name ( ) ,
" Code " : { " ImageUri " : image_uri } ,
" PackageType " : " Image " ,
" Timeout " : 100 ,
}
2022-11-23 13:16:33 +00:00
2022-06-10 14:09:13 +00:00
resp = lambda_client . create_function ( * * dic )
2023-06-11 18:44:30 +00:00
assert resp [ " FunctionName " ] == fn_name
assert resp [ " CodeSize " ] == 0
assert " CodeSha256 " in resp
assert resp [ " PackageType " ] == " Image "
2022-06-10 14:09:13 +00:00
result = lambda_client . get_function ( FunctionName = fn_name )
2023-06-11 18:44:30 +00:00
assert " Configuration " in result
2022-06-10 14:09:13 +00:00
config = result [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert " Code " in result
2022-06-10 14:09:13 +00:00
code = result [ " Code " ]
2023-06-11 18:44:30 +00:00
assert code [ " RepositoryType " ] == " ECR "
assert code [ " ImageUri " ] == image_uri
2022-06-10 14:09:13 +00:00
image_uri_without_tag = image_uri . split ( " : " ) [ 0 ]
resolved_image_uri = f " { image_uri_without_tag } @sha256: { config [ ' CodeSha256 ' ] } "
2023-06-11 18:44:30 +00:00
assert code [ " ResolvedImageUri " ] == resolved_image_uri
2022-06-10 14:09:13 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-12-16 18:22:43 +00:00
def test_create_function_from_mocked_ecr_image_tag (
2022-11-23 13:16:33 +00:00
with_ecr_mock ,
) : # pylint: disable=unused-argument
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2022-11-23 13:16:33 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
lambda_client = boto3 . client ( " lambda " , " us-east-1 " )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2022-12-16 18:22:43 +00:00
image = with_ecr_mock
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr: { image [ ' imageTag ' ] } "
2022-11-23 13:16:33 +00:00
dic = {
" FunctionName " : fn_name ,
" Role " : get_role_name ( ) ,
" Code " : { " ImageUri " : image_uri } ,
" PackageType " : " Image " ,
" Timeout " : 100 ,
}
resp = lambda_client . create_function ( * * dic )
2023-06-11 18:44:30 +00:00
assert resp [ " FunctionName " ] == fn_name
assert resp [ " CodeSize " ] > 0
assert " CodeSha256 " in resp
assert resp [ " PackageType " ] == " Image "
2022-11-23 13:16:33 +00:00
result = lambda_client . get_function ( FunctionName = fn_name )
2023-06-11 18:44:30 +00:00
assert " Configuration " in result
2022-11-23 13:16:33 +00:00
config = result [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert config [ " CodeSha256 " ] == image [ " imageDigest " ] . replace ( " sha256: " , " " )
assert config [ " CodeSize " ] == resp [ " CodeSize " ]
assert " Code " in result
2022-11-23 13:16:33 +00:00
code = result [ " Code " ]
2023-06-11 18:44:30 +00:00
assert code [ " RepositoryType " ] == " ECR "
assert code [ " ImageUri " ] == image_uri
2022-11-23 13:16:33 +00:00
image_uri_without_tag = image_uri . split ( " : " ) [ 0 ]
resolved_image_uri = f " { image_uri_without_tag } @sha256: { config [ ' CodeSha256 ' ] } "
2023-06-11 18:44:30 +00:00
assert code [ " ResolvedImageUri " ] == resolved_image_uri
2022-11-23 13:16:33 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-12-16 18:22:43 +00:00
def test_create_function_from_mocked_ecr_image_digest (
with_ecr_mock ,
) : # pylint: disable=unused-argument
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2022-12-16 18:22:43 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
lambda_client = boto3 . client ( " lambda " , " us-east-1 " )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image = with_ecr_mock
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr@ { image [ ' imageDigest ' ] } "
dic = {
" FunctionName " : fn_name ,
" Role " : get_role_name ( ) ,
" Code " : { " ImageUri " : image_uri } ,
" PackageType " : " Image " ,
" Timeout " : 100 ,
}
resp = lambda_client . create_function ( * * dic )
2023-06-11 18:44:30 +00:00
assert resp [ " FunctionName " ] == fn_name
assert resp [ " CodeSize " ] > 0
assert resp [ " CodeSha256 " ] == image [ " imageDigest " ] . replace ( " sha256: " , " " )
assert resp [ " PackageType " ] == " Image "
2022-12-16 18:22:43 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-11-23 13:16:33 +00:00
def test_create_function_from_mocked_ecr_missing_image (
with_ecr_mock ,
) : # pylint: disable=unused-argument
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2022-11-23 13:16:33 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
lambda_client = boto3 . client ( " lambda " , " us-east-1 " )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:dne "
dic = {
" FunctionName " : fn_name ,
" Role " : get_role_name ( ) ,
" Code " : { " ImageUri " : image_uri } ,
" PackageType " : " Image " ,
" Timeout " : 100 ,
}
with pytest . raises ( ClientError ) as exc :
lambda_client . create_function ( * * dic )
err = exc . value . response [ " Error " ]
2023-06-11 18:44:30 +00:00
assert err [ " Code " ] == " ImageNotFoundException "
assert (
err [ " Message " ]
== " The image with imageId { ' imageTag ' : ' dne ' } does not exist within the repository with name ' testlambdaecr ' in the registry with id ' 123456789012 ' "
2022-11-23 13:16:33 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-12 19:39:20 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_get_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 21:24:17 +00:00
2016-10-06 09:52:23 +00:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 19:39:20 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 23:04:58 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 19:39:20 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
2019-11-04 15:22:03 +00:00
Environment = { " Variables " : { " test_variable " : " test_value " } } ,
2016-02-12 19:39:20 +00:00
)
2021-09-21 15:19:49 +00:00
result = conn . get_function ( FunctionName = function_name )
2017-02-24 02:37:43 +00:00
# this is hard to match against, so remove it
result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2017-11-26 21:28:28 +00:00
result [ " Configuration " ] . pop ( " LastModified " )
2016-02-12 19:39:20 +00:00
2023-06-11 18:44:30 +00:00
assert (
result [ " Code " ] [ " Location " ]
== f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com/test.zip "
)
assert result [ " Code " ] [ " RepositoryType " ] == " S3 "
assert result [ " Configuration " ] [ " CodeSha256 " ] == base64 . b64encode (
hashlib . sha256 ( zip_content ) . digest ( )
) . decode ( " utf-8 " )
assert result [ " Configuration " ] [ " CodeSize " ] == len ( zip_content )
assert result [ " Configuration " ] [ " Description " ] == " test lambda function "
assert " FunctionArn " in result [ " Configuration " ]
assert result [ " Configuration " ] [ " FunctionName " ] == function_name
assert result [ " Configuration " ] [ " Handler " ] == " lambda_function.lambda_handler "
assert result [ " Configuration " ] [ " MemorySize " ] == 128
assert result [ " Configuration " ] [ " Role " ] == get_role_name ( )
2023-08-31 06:47:49 +00:00
assert result [ " Configuration " ] [ " Runtime " ] == PYTHON_VERSION
2023-06-11 18:44:30 +00:00
assert result [ " Configuration " ] [ " Timeout " ] == 3
assert result [ " Configuration " ] [ " Version " ] == " $LATEST "
assert " VpcConfig " in result [ " Configuration " ]
assert " Environment " in result [ " Configuration " ]
assert " Variables " in result [ " Configuration " ] [ " Environment " ]
assert result [ " Configuration " ] [ " Environment " ] [ " Variables " ] == {
" test_variable " : " test_value "
}
2017-11-26 21:28:28 +00:00
2020-08-26 10:06:53 +00:00
# Test get function with qualifier
2021-09-21 15:19:49 +00:00
result = conn . get_function ( FunctionName = function_name , Qualifier = " $LATEST " )
2023-06-11 18:44:30 +00:00
assert result [ " Configuration " ] [ " Version " ] == " $LATEST "
assert (
result [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :$LATEST "
2019-10-31 15:44:26 +00:00
)
2016-02-12 19:39:20 +00:00
2023-11-10 19:13:13 +00:00
# Test get function with version
result = conn . get_function ( FunctionName = function_name , Qualifier = " 1 " )
assert result [ " Configuration " ] [ " Version " ] == " 1 "
assert (
result [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :1 "
)
# Test get function with version inside of name
result = conn . get_function ( FunctionName = f " { function_name } :1 " )
assert result [ " Configuration " ] [ " Version " ] == " 1 "
assert (
result [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :1 "
)
2019-02-18 04:32:39 +00:00
# Test get function when can't find function name
2020-10-06 05:54:49 +00:00
with pytest . raises ( conn . exceptions . ResourceNotFoundException ) :
2019-02-18 04:32:39 +00:00
conn . get_function ( FunctionName = " junk " , Qualifier = " $LATEST " )
2019-11-04 15:22:03 +00:00
2021-08-28 06:23:44 +00:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2024-01-07 12:03:33 +00:00
@mock_aws
2021-01-10 15:24:04 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
2021-08-28 06:23:44 +00:00
def test_get_function_configuration ( key ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2021-01-10 15:24:04 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2021-01-10 15:24:04 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2021-01-10 15:24:04 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2021-01-10 15:24:04 +00:00
2021-08-28 06:23:44 +00:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2021-01-10 15:24:04 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2021-01-10 15:24:04 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
Environment = { " Variables " : { " test_variable " : " test_value " } } ,
)
2021-08-28 06:23:44 +00:00
name_or_arn = fxn [ key ]
2021-01-10 15:24:04 +00:00
2021-08-28 06:23:44 +00:00
result = conn . get_function_configuration ( FunctionName = name_or_arn )
2021-01-10 15:24:04 +00:00
2023-06-11 18:44:30 +00:00
assert result [ " CodeSha256 " ] == base64 . b64encode (
hashlib . sha256 ( zip_content ) . digest ( )
) . decode ( " utf-8 " )
assert result [ " CodeSize " ] == len ( zip_content )
assert result [ " Description " ] == " test lambda function "
assert " FunctionArn " in result
assert result [ " FunctionName " ] == function_name
assert result [ " Handler " ] == " lambda_function.lambda_handler "
assert result [ " MemorySize " ] == 128
assert result [ " Role " ] == get_role_name ( )
2023-08-31 06:47:49 +00:00
assert result [ " Runtime " ] == PYTHON_VERSION
2023-06-11 18:44:30 +00:00
assert result [ " Timeout " ] == 3
assert result [ " Version " ] == " $LATEST "
assert " VpcConfig " in result
assert " Environment " in result
assert " Variables " in result [ " Environment " ]
assert result [ " Environment " ] [ " Variables " ] == { " test_variable " : " test_value " }
2021-01-10 15:24:04 +00:00
# Test get function with qualifier
result = conn . get_function_configuration (
2021-08-28 06:23:44 +00:00
FunctionName = name_or_arn , Qualifier = " $LATEST "
2021-01-10 15:24:04 +00:00
)
2023-06-11 18:44:30 +00:00
assert result [ " Version " ] == " $LATEST "
assert (
result [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :$LATEST "
2021-01-10 15:24:04 +00:00
)
# Test get function when can't find function name
with pytest . raises ( conn . exceptions . ResourceNotFoundException ) :
conn . get_function_configuration ( FunctionName = " junk " , Qualifier = " $LATEST " )
2022-02-08 21:12:51 +00:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2024-01-07 12:03:33 +00:00
@mock_aws
2022-02-08 21:12:51 +00:00
def test_get_function_code_signing_config ( key ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2022-02-08 21:12:51 +00:00
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 )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
fxn = conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2022-02-08 21:12:51 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
CodeSigningConfigArn = " csc:arn " ,
)
name_or_arn = fxn [ key ]
result = conn . get_function_code_signing_config ( FunctionName = name_or_arn )
2023-06-11 18:44:30 +00:00
assert result [ " FunctionName " ] == function_name
assert result [ " CodeSigningConfigArn " ] == " csc:arn "
2022-02-08 21:12:51 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-04 13:44:01 +00:00
def test_get_function_by_arn ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2019-11-04 15:22:03 +00:00
s3_conn = boto3 . client ( " s3 " , " us-east-1 " )
2020-02-02 11:48:32 +00:00
s3_conn . create_bucket (
Bucket = bucket_name ,
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-11-04 13:44:01 +00:00
zip_content = get_test_zip_file2 ( )
2019-11-04 15:22:03 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , " us-east-1 " )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-11-04 13:44:01 +00:00
2019-11-04 15:22:03 +00:00
fnc = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-11-04 15:22:03 +00:00
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2019-11-04 13:44:01 +00:00
2019-11-04 15:22:03 +00:00
result = conn . get_function ( FunctionName = fnc [ " FunctionArn " ] )
2023-06-11 18:44:30 +00:00
assert result [ " Configuration " ] [ " FunctionName " ] == function_name
2019-11-04 13:44:01 +00:00
2023-11-10 19:13:13 +00:00
# Test with version
result = conn . get_function ( FunctionName = fnc [ " FunctionArn " ] , Qualifier = " 1 " )
assert (
result [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { function_name } :1 "
)
assert result [ " Configuration " ] [ " Version " ] == " 1 "
# Test with version inside of ARN
result = conn . get_function ( FunctionName = f " { fnc [ ' FunctionArn ' ] } :1 " )
assert result [ " Configuration " ] [ " Version " ] == " 1 "
assert (
result [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { function_name } :1 "
)
2019-02-18 04:32:39 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-12 19:39:20 +00:00
def test_delete_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 21:24:17 +00:00
2016-10-06 09:52:23 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 19:39:20 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 23:04:58 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 19:39:20 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
success_result = conn . delete_function ( FunctionName = function_name )
2017-02-24 02:37:43 +00:00
# this is hard to match against, so remove it
success_result [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
success_result [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2024-01-14 13:01:30 +00:00
success_result [ " ResponseMetadata " ] . pop ( " RequestId " )
2016-10-09 15:13:52 +00:00
2023-06-11 18:44:30 +00:00
assert success_result == { " ResponseMetadata " : { " HTTPStatusCode " : 204 } }
2016-02-12 19:39:20 +00:00
2023-11-10 19:13:13 +00:00
func_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
2021-09-21 15:19:49 +00:00
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 0
2019-10-22 08:31:37 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-10-22 08:31:37 +00:00
def test_delete_function_by_arn ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2019-10-22 08:31:37 +00:00
s3_conn = boto3 . client ( " s3 " , " us-east-1 " )
2020-02-02 11:48:32 +00:00
s3_conn . create_bucket (
Bucket = bucket_name ,
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-22 08:31:37 +00:00
zip_content = get_test_zip_file2 ( )
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , " us-east-1 " )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-31 15:44:26 +00:00
2019-10-22 08:31:37 +00:00
fnc = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-22 08:31:37 +00:00
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
conn . delete_function ( FunctionName = fnc [ " FunctionArn " ] )
2021-09-21 15:19:49 +00:00
func_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in func_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 0
2019-10-22 08:31:37 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-10-22 08:31:37 +00:00
def test_delete_unknown_function ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2023-06-11 18:44:30 +00:00
with pytest . raises ( ClientError ) as exc :
conn . delete_function ( FunctionName = " testFunctionThatDoesntExist " )
err = exc . value . response [ " Error " ]
assert err [ " Code " ] == " ResourceNotFoundException "
2016-02-12 19:39:20 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-03-15 15:07:01 +00:00
@pytest.mark.parametrize (
" name " ,
[
" bad_function_name " ,
f " arn:aws:lambda:eu-west-1: { ACCOUNT_ID } :function:bad_function_name " ,
] ,
)
def test_publish_version_unknown_function ( name ) :
client = boto3 . client ( " lambda " , " eu-west-1 " )
with pytest . raises ( ClientError ) as exc :
client . publish_version ( FunctionName = name , Description = " v2 " )
err = exc . value . response [ " Error " ]
2023-06-11 18:44:30 +00:00
assert err [ " Code " ] == " ResourceNotFoundException "
assert (
err [ " Message " ]
== f " Function not found: arn:aws:lambda:eu-west-1: { ACCOUNT_ID } :function:bad_function_name "
2022-03-15 15:07:01 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2017-11-26 21:28:28 +00:00
def test_publish ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2017-11-26 21:28:28 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2017-11-26 21:28:28 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-11-26 21:28:28 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2017-11-26 21:28:28 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
2019-05-21 16:49:56 +00:00
Publish = False ,
2017-11-26 21:28:28 +00:00
)
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 1
2021-09-21 15:19:49 +00:00
latest_arn = our_functions [ 0 ] [ " FunctionArn " ]
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
res = conn . publish_version ( FunctionName = function_name )
2019-03-29 06:52:47 +00:00
assert res [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 201
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 2
2017-11-26 21:28:28 +00:00
# #SetComprehension ;-)
2021-09-21 15:19:49 +00:00
published_arn = list ( { f [ " FunctionArn " ] for f in our_functions } - { latest_arn } ) [ 0 ]
2023-06-11 18:44:30 +00:00
assert f " { function_name } :1 " in published_arn
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
conn . delete_function ( FunctionName = function_name , Qualifier = " 1 " )
2017-11-26 21:28:28 +00:00
2021-09-21 15:19:49 +00:00
function_list = conn . list_functions ( ) [ " Functions " ]
our_functions = [ f for f in function_list if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 1
assert function_name in our_functions [ 0 ] [ " FunctionArn " ]
2017-11-26 21:28:28 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2016-02-12 19:39:20 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_list_create_list_get_delete_list ( ) :
"""
test ` list - > create - > list - > get - > delete - > list ` integration
"""
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2016-02-17 21:24:17 +00:00
2016-10-06 09:52:23 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2016-02-12 19:39:20 +00:00
2021-09-21 15:19:49 +00:00
initial_list = conn . list_functions ( ) [ " Functions " ]
initial_names = [ f [ " FunctionName " ] for f in initial_list ]
2023-06-11 18:44:30 +00:00
assert function_name not in initial_names
2016-02-12 19:39:20 +00:00
2021-09-21 15:19:49 +00:00
function_name = function_name
2016-02-12 19:39:20 +00:00
conn . create_function (
2021-08-28 14:26:44 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-27 23:04:58 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2016-02-12 19:39:20 +00:00
Description = " test lambda function " ,
2023-07-06 16:45:29 +00:00
EphemeralStorage = { " Size " : 2500 } ,
2016-02-12 19:39:20 +00:00
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
expected_function_result = {
" Code " : {
2022-11-17 22:41:08 +00:00
" Location " : f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com/test.zip " ,
2016-02-12 19:39:20 +00:00
" RepositoryType " : " S3 " ,
} ,
" Configuration " : {
2022-03-17 12:32:31 +00:00
" CodeSha256 " : base64 . b64encode ( hashlib . sha256 ( zip_content ) . digest ( ) ) . decode (
" utf-8 "
) ,
2016-02-17 21:24:17 +00:00
" CodeSize " : len ( zip_content ) ,
2016-02-12 19:39:20 +00:00
" Description " : " test lambda function " ,
2021-08-28 14:26:44 +00:00
" FunctionName " : function_name ,
2017-09-27 23:04:58 +00:00
" Handler " : " lambda_function.lambda_handler " ,
2016-02-12 19:39:20 +00:00
" MemorySize " : 128 ,
2023-12-10 20:31:33 +00:00
" PackageType " : " Zip " ,
2019-11-07 17:11:13 +00:00
" Role " : get_role_name ( ) ,
2023-08-31 06:47:49 +00:00
" Runtime " : PYTHON_VERSION ,
2016-02-12 19:39:20 +00:00
" Timeout " : 3 ,
" Version " : " $LATEST " ,
2016-02-16 20:35:29 +00:00
" VpcConfig " : { " SecurityGroupIds " : [ ] , " SubnetIds " : [ ] } ,
2020-01-23 18:46:24 +00:00
" State " : " Active " ,
2021-01-17 15:28:49 +00:00
" Layers " : [ ] ,
2022-03-17 12:32:31 +00:00
" LastUpdateStatus " : " Successful " ,
" TracingConfig " : { " Mode " : " PassThrough " } ,
2023-07-06 16:45:29 +00:00
" Architectures " : [ " x86_64 " ] ,
" EphemeralStorage " : { " Size " : 2500 } ,
" SnapStart " : { " ApplyOn " : " None " , " OptimizationStatus " : " Off " } ,
2016-02-12 19:39:20 +00:00
} ,
" ResponseMetadata " : { " HTTPStatusCode " : 200 } ,
}
2021-08-28 14:26:44 +00:00
functions = conn . list_functions ( ) [ " Functions " ]
2021-09-21 15:19:49 +00:00
func_names = [ f [ " FunctionName " ] for f in functions ]
2023-06-11 18:44:30 +00:00
assert function_name in func_names
2021-09-21 15:19:49 +00:00
func_arn = [
f [ " FunctionArn " ] for f in functions if f [ " FunctionName " ] == function_name
] [ 0 ]
2023-06-11 18:44:30 +00:00
assert (
func_arn
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2021-08-28 14:26:44 +00:00
)
functions = conn . list_functions ( FunctionVersion = " ALL " ) [ " Functions " ]
2021-09-21 15:19:49 +00:00
our_functions = [ f for f in functions if f [ " FunctionName " ] == function_name ]
2023-06-11 18:44:30 +00:00
assert len ( our_functions ) == 2
2021-08-28 14:26:44 +00:00
2021-09-21 15:19:49 +00:00
latest = [ f for f in our_functions if f [ " Version " ] == " $LATEST " ] [ 0 ]
2023-06-11 18:44:30 +00:00
assert (
latest [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :$LATEST "
2021-08-28 14:26:44 +00:00
)
latest . pop ( " FunctionArn " )
latest . pop ( " LastModified " )
2023-06-11 18:44:30 +00:00
assert latest == expected_function_result [ " Configuration " ]
2021-08-28 14:26:44 +00:00
2021-09-21 15:19:49 +00:00
published = [ f for f in our_functions if f [ " Version " ] != " $LATEST " ] [ 0 ]
2023-06-11 18:44:30 +00:00
assert published [ " Version " ] == " 1 "
assert (
published [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :1 "
2021-08-28 14:26:44 +00:00
)
func = conn . get_function ( FunctionName = function_name )
2023-06-11 18:44:30 +00:00
assert (
func [ " Configuration " ] [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
2021-08-28 14:26:44 +00:00
)
2016-02-12 19:39:20 +00:00
2017-02-24 02:37:43 +00:00
# this is hard to match against, so remove it
func [ " ResponseMetadata " ] . pop ( " HTTPHeaders " , None )
# Botocore inserts retry attempts not seen in Python27
func [ " ResponseMetadata " ] . pop ( " RetryAttempts " , None )
2024-01-14 13:01:30 +00:00
func [ " ResponseMetadata " ] . pop ( " RequestId " )
2017-02-24 00:43:48 +00:00
func [ " Configuration " ] . pop ( " LastModified " )
2021-08-28 14:26:44 +00:00
func [ " Configuration " ] . pop ( " FunctionArn " )
2016-10-09 15:13:52 +00:00
2023-06-11 18:44:30 +00:00
assert func == expected_function_result
2021-09-21 15:19:49 +00:00
conn . delete_function ( FunctionName = function_name )
2017-03-17 02:00:57 +00:00
2021-09-21 15:19:49 +00:00
functions = conn . list_functions ( ) [ " Functions " ]
func_names = [ f [ " FunctionName " ] for f in functions ]
2023-06-11 18:44:30 +00:00
assert function_name not in func_names
2017-09-13 04:44:22 +00:00
2017-10-25 19:04:00 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2017-09-15 02:49:32 +00:00
@freeze_time ( " 2015-01-01 00:00:00 " )
def test_get_function_created_with_zipfile ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2017-09-15 02:49:32 +00:00
zip_content = get_test_zip_file1 ( )
2021-10-18 19:44:29 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2017-09-15 02:49:32 +00:00
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
)
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name )
2017-09-15 02:49:32 +00:00
2023-06-11 18:44:30 +00:00
assert response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
2017-09-27 23:04:58 +00:00
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 22:41:08 +00:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 15:44:26 +00:00
)
2023-06-11 18:44:30 +00:00
assert " Configuration " in response
2022-03-17 12:32:31 +00:00
config = response [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert config [ " CodeSha256 " ] == base64 . b64encode (
hashlib . sha256 ( zip_content ) . digest ( )
) . decode ( " utf-8 " )
assert config [ " CodeSize " ] == len ( zip_content )
assert config [ " Description " ] == " test lambda function "
assert (
config [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } "
)
assert config [ " FunctionName " ] == function_name
assert config [ " Handler " ] == " lambda_function.handler "
assert config [ " MemorySize " ] == 128
assert config [ " Role " ] == get_role_name ( )
2023-08-31 06:47:49 +00:00
assert config [ " Runtime " ] == PYTHON_VERSION
2023-06-11 18:44:30 +00:00
assert config [ " Timeout " ] == 3
assert config [ " Version " ] == " $LATEST "
assert config [ " State " ] == " Active "
assert config [ " Layers " ] == [ ]
assert config [ " LastUpdateStatus " ] == " Successful "
2019-02-16 15:27:23 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-02-16 15:27:23 +00:00
def test_list_versions_by_function ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-02-16 15:27:23 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-02-16 15:27:23 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-02-16 15:27:23 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-02-16 15:27:23 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2022-12-05 10:31:20 +00:00
conn . update_function_code ( FunctionName = function_name , ZipFile = get_test_zip_file1 ( ) )
2019-02-16 15:27:23 +00:00
2021-09-21 15:19:49 +00:00
res = conn . publish_version ( FunctionName = function_name )
2019-03-29 06:52:47 +00:00
assert res [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 201
2021-09-21 15:19:49 +00:00
versions = conn . list_versions_by_function ( FunctionName = function_name )
2019-05-21 16:49:56 +00:00
assert len ( versions [ " Versions " ] ) == 3
2022-11-17 22:41:08 +00:00
assert (
versions [ " Versions " ] [ 0 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :$LATEST "
)
assert (
versions [ " Versions " ] [ 1 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :1 "
)
assert (
versions [ " Versions " ] [ 2 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :2 "
)
2019-05-21 16:49:56 +00:00
conn . create_function (
FunctionName = " testFunction_2 " ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-05-21 16:49:56 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-05-21 16:49:56 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = False ,
)
versions = conn . list_versions_by_function ( FunctionName = " testFunction_2 " )
assert len ( versions [ " Versions " ] ) == 1
2022-11-17 22:41:08 +00:00
assert (
versions [ " Versions " ] [ 0 ] [ " FunctionArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function:testFunction_2:$LATEST "
2019-10-31 15:44:26 +00:00
)
2019-02-16 15:27:23 +00:00
2019-02-16 18:37:46 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-07-01 10:32:33 +00:00
def test_list_aliases ( ) :
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_file2 ( )
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
function_name2 = str ( uuid4 ( ) ) [ 0 : 6 ]
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-07-01 10:32:33 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
conn . create_function (
FunctionName = function_name2 ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-07-01 10:32:33 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
first_version = conn . publish_version ( FunctionName = function_name ) [ " Version " ]
conn . create_alias (
FunctionName = function_name ,
Name = " alias1 " ,
FunctionVersion = first_version ,
)
conn . update_function_code ( FunctionName = function_name , ZipFile = get_test_zip_file1 ( ) )
second_version = conn . publish_version ( FunctionName = function_name ) [ " Version " ]
conn . create_alias (
FunctionName = function_name ,
Name = " alias2 " ,
FunctionVersion = second_version ,
)
conn . create_alias (
FunctionName = function_name ,
Name = " alias0 " ,
FunctionVersion = second_version ,
)
aliases = conn . list_aliases ( FunctionName = function_name )
assert len ( aliases [ " Aliases " ] ) == 3
# should be ordered by their alias name (as per SDK response)
assert (
aliases [ " Aliases " ] [ 0 ] [ " AliasArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :alias0 "
)
assert aliases [ " Aliases " ] [ 0 ] [ " FunctionVersion " ] == second_version
assert (
aliases [ " Aliases " ] [ 1 ] [ " AliasArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :alias1 "
)
assert aliases [ " Aliases " ] [ 1 ] [ " FunctionVersion " ] == first_version
assert (
aliases [ " Aliases " ] [ 2 ] [ " AliasArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name } :alias2 "
)
assert aliases [ " Aliases " ] [ 2 ] [ " FunctionVersion " ] == second_version
res = conn . publish_version ( FunctionName = function_name2 )
conn . create_alias (
FunctionName = function_name2 ,
Name = " alias1 " ,
FunctionVersion = res [ " Version " ] ,
)
aliases = conn . list_aliases ( FunctionName = function_name2 )
assert len ( aliases [ " Aliases " ] ) == 1
assert (
aliases [ " Aliases " ] [ 0 ] [ " AliasArn " ]
== f " arn:aws:lambda:us-west-2: { ACCOUNT_ID } :function: { function_name2 } :alias1 "
)
2024-01-07 12:03:33 +00:00
@mock_aws
2019-02-16 18:37:46 +00:00
def test_create_function_with_already_exists ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-02-16 18:37:46 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-02-16 18:37:46 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-02-16 18:37:46 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-02-16 18:37:46 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2023-08-15 21:24:01 +00:00
with pytest . raises ( ClientError ) as exc :
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 21:24:01 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2019-02-16 18:37:46 +00:00
2023-08-15 21:24:01 +00:00
assert exc . value . response [ " Error " ] [ " Code " ] == " ResourceConflictException "
2019-02-16 18:37:46 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-02-16 18:37:46 +00:00
def test_list_versions_by_function_for_nonexistent_function ( ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
versions = conn . list_versions_by_function ( FunctionName = function_name )
2019-02-16 18:37:46 +00:00
2019-02-18 14:52:37 +00:00
assert len ( versions [ " Versions " ] ) == 0
2019-08-21 01:54:57 +00:00
2021-08-28 06:23:44 +00:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-28 06:23:44 +00:00
def test_update_configuration ( key ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-08 20:59:03 +00:00
zip_content = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2019-10-08 20:59:03 +00:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-08 20:59:03 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-10-08 20:59:03 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
2019-11-04 15:22:03 +00:00
Environment = { " Variables " : { " test_old_environment " : " test_old_value " } } ,
2019-10-08 20:59:03 +00:00
)
2021-08-28 06:23:44 +00:00
name_or_arn = fxn [ key ]
2019-10-08 20:59:03 +00:00
assert fxn [ " Description " ] == " test lambda function "
assert fxn [ " Handler " ] == " lambda_function.lambda_handler "
assert fxn [ " MemorySize " ] == 128
2023-08-31 06:47:49 +00:00
assert fxn [ " Runtime " ] == PYTHON_VERSION
2019-10-08 20:59:03 +00:00
assert fxn [ " Timeout " ] == 3
updated_config = conn . update_function_configuration (
2021-08-28 06:23:44 +00:00
FunctionName = name_or_arn ,
2019-10-08 20:59:03 +00:00
Description = " updated test lambda function " ,
Handler = " lambda_function.new_lambda_handler " ,
Runtime = " python3.6 " ,
2019-11-04 13:44:01 +00:00
Timeout = 7 ,
2020-11-18 08:45:31 +00:00
VpcConfig = { " SecurityGroupIds " : [ " sg-123abc " ] , " SubnetIds " : [ " subnet-123abc " ] } ,
2019-11-04 15:22:03 +00:00
Environment = { " Variables " : { " test_environment " : " test_value " } } ,
2019-10-08 20:59:03 +00:00
)
assert updated_config [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
assert updated_config [ " Description " ] == " updated test lambda function "
assert updated_config [ " Handler " ] == " lambda_function.new_lambda_handler "
assert updated_config [ " MemorySize " ] == 128
assert updated_config [ " Runtime " ] == " python3.6 "
assert updated_config [ " Timeout " ] == 7
2019-11-04 15:22:03 +00:00
assert updated_config [ " Environment " ] [ " Variables " ] == {
" test_environment " : " test_value "
}
2020-11-18 08:45:31 +00:00
assert updated_config [ " VpcConfig " ] == {
" SecurityGroupIds " : [ " sg-123abc " ] ,
" SubnetIds " : [ " subnet-123abc " ] ,
" VpcId " : " vpc-123abc " ,
}
2019-10-08 20:59:03 +00:00
2021-08-28 06:23:44 +00:00
@pytest.mark.parametrize ( " key " , [ " FunctionName " , " FunctionArn " ] )
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-28 06:23:44 +00:00
def test_update_function_zip ( key ) :
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2019-10-08 20:59:03 +00:00
zip_content_one = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-08 20:59:03 +00:00
fxn = conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-08 20:59:03 +00:00
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content_one } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
2021-08-28 06:23:44 +00:00
name_or_arn = fxn [ key ]
2022-12-05 10:31:20 +00:00
first_sha = fxn [ " CodeSha256 " ]
2019-10-08 20:59:03 +00:00
zip_content_two = get_test_zip_file2 ( )
2022-12-05 10:31:20 +00:00
update1 = conn . update_function_code (
2021-08-28 06:23:44 +00:00
FunctionName = name_or_arn , ZipFile = zip_content_two , Publish = True
2019-10-08 20:59:03 +00:00
)
2023-06-11 18:44:30 +00:00
assert update1 [ " CodeSha256 " ] != first_sha
2019-10-08 20:59:03 +00:00
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name , Qualifier = " 2 " )
2019-10-08 20:59:03 +00:00
2023-06-11 18:44:30 +00:00
assert response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
2019-10-08 20:59:03 +00:00
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 22:41:08 +00:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 15:44:26 +00:00
)
2022-03-17 12:32:31 +00:00
config = response [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert config [ " CodeSize " ] == len ( zip_content_two )
assert config [ " Description " ] == " test lambda function "
assert (
config [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :2 "
2019-10-09 21:20:49 +00:00
)
2023-06-11 18:44:30 +00:00
assert config [ " FunctionName " ] == function_name
assert config [ " Version " ] == " 2 "
assert config [ " LastUpdateStatus " ] == " Successful "
assert config [ " CodeSha256 " ] == update1 [ " CodeSha256 " ]
2022-12-05 10:31:20 +00:00
most_recent_config = conn . get_function ( FunctionName = function_name )
2023-06-11 18:44:30 +00:00
assert most_recent_config [ " Configuration " ] [ " CodeSha256 " ] == update1 [ " CodeSha256 " ]
2022-12-05 10:31:20 +00:00
# Publishing this again, with the same code, gives us the same version
same_update = conn . update_function_code (
FunctionName = name_or_arn , ZipFile = zip_content_two , Publish = True
)
2023-06-11 18:44:30 +00:00
assert (
same_update [ " FunctionArn " ]
== most_recent_config [ " Configuration " ] [ " FunctionArn " ] + " :2 "
2022-12-05 10:31:20 +00:00
)
2023-06-11 18:44:30 +00:00
assert same_update [ " Version " ] == " 2 "
2022-12-05 10:31:20 +00:00
# Only when updating the code should we have a new version
new_update = conn . update_function_code (
FunctionName = name_or_arn , ZipFile = get_test_zip_file3 ( ) , Publish = True
)
2023-06-11 18:44:30 +00:00
assert (
new_update [ " FunctionArn " ]
== most_recent_config [ " Configuration " ] [ " FunctionArn " ] + " :3 "
2022-12-05 10:31:20 +00:00
)
2023-06-11 18:44:30 +00:00
assert new_update [ " Version " ] == " 3 "
2019-10-09 21:20:49 +00:00
2019-10-31 15:44:26 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-10-09 21:20:49 +00:00
def test_update_function_s3 ( ) :
2021-09-21 15:19:49 +00:00
bucket_name = str ( uuid4 ( ) )
2020-02-02 11:48:32 +00:00
s3_conn = boto3 . client ( " s3 " , _lambda_region )
s3_conn . create_bucket (
2021-09-21 15:19:49 +00:00
Bucket = bucket_name ,
2020-02-02 11:48:32 +00:00
CreateBucketConfiguration = { " LocationConstraint " : _lambda_region } ,
)
2019-10-09 21:20:49 +00:00
zip_content = get_test_zip_file1 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test.zip " , Body = zip_content )
2019-10-09 21:20:49 +00:00
2020-02-02 11:48:32 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
2021-09-21 15:19:49 +00:00
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
2019-10-09 21:20:49 +00:00
2022-03-17 12:32:31 +00:00
conn . create_function (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2019-11-07 17:11:13 +00:00
Role = get_role_name ( ) ,
2019-10-09 21:20:49 +00:00
Handler = " lambda_function.lambda_handler " ,
2021-09-21 15:19:49 +00:00
Code = { " S3Bucket " : bucket_name , " S3Key " : " test.zip " } ,
2019-10-09 21:20:49 +00:00
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
zip_content_two = get_test_zip_file2 ( )
2021-09-21 15:19:49 +00:00
s3_conn . put_object ( Bucket = bucket_name , Key = " test2.zip " , Body = zip_content_two )
2019-10-09 21:20:49 +00:00
2021-10-18 19:44:29 +00:00
conn . update_function_code (
2021-09-21 15:19:49 +00:00
FunctionName = function_name ,
S3Bucket = bucket_name ,
2019-10-09 21:20:49 +00:00
S3Key = " test2.zip " ,
Publish = True ,
)
2021-09-21 15:19:49 +00:00
response = conn . get_function ( FunctionName = function_name , Qualifier = " 2 " )
2019-10-09 21:20:49 +00:00
2023-06-11 18:44:30 +00:00
assert response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
2019-10-09 21:20:49 +00:00
assert len ( response [ " Code " ] ) == 2
assert response [ " Code " ] [ " RepositoryType " ] == " S3 "
assert response [ " Code " ] [ " Location " ] . startswith (
2022-11-17 22:41:08 +00:00
f " s3://awslambda- { _lambda_region } -tasks.s3- { _lambda_region } .amazonaws.com "
2019-10-31 15:44:26 +00:00
)
2022-03-17 12:32:31 +00:00
config = response [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert config [ " CodeSha256 " ] == base64 . b64encode (
hashlib . sha256 ( zip_content_two ) . digest ( )
) . decode ( " utf-8 " )
assert config [ " CodeSize " ] == len ( zip_content_two )
assert config [ " Description " ] == " test lambda function "
assert (
config [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :2 "
2019-10-08 20:59:03 +00:00
)
2023-06-11 18:44:30 +00:00
assert config [ " FunctionName " ] == function_name
assert config [ " Version " ] == " 2 "
assert config [ " LastUpdateStatus " ] == " Successful "
2019-11-07 17:11:13 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-08 10:11:24 +00:00
def test_update_function_ecr ( ) :
2024-01-17 10:02:15 +00:00
if LooseVersion ( boto3_version ) < LooseVersion ( " 1.29.0 " ) :
raise SkipTest ( " Parameters only available in newer versions " )
2023-08-08 10:11:24 +00:00
conn = boto3 . client ( " lambda " , _lambda_region )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
image_uri = f " { ACCOUNT_ID } .dkr.ecr.us-east-1.amazonaws.com/testlambdaecr:prod "
image_config = {
" EntryPoint " : [
" python " ,
] ,
" Command " : [
" /opt/app.py " ,
] ,
" WorkingDirectory " : " /opt " ,
}
conn . create_function (
FunctionName = function_name ,
Role = get_role_name ( ) ,
Code = { " ImageUri " : image_uri } ,
Description = " test lambda function " ,
ImageConfig = image_config ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
new_uri = image_uri . replace ( " prod " , " newer " )
conn . update_function_code (
FunctionName = function_name ,
ImageUri = new_uri ,
Publish = True ,
)
response = conn . get_function ( FunctionName = function_name , Qualifier = " 2 " )
assert response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 200
assert len ( response [ " Code " ] ) == 3
assert response [ " Code " ] [ " RepositoryType " ] == " ECR "
assert response [ " Code " ] [ " ImageUri " ] == new_uri
assert response [ " Code " ] [ " ResolvedImageUri " ] . endswith (
hashlib . sha256 ( new_uri . encode ( " utf-8 " ) ) . hexdigest ( )
)
config = response [ " Configuration " ]
assert config [ " CodeSize " ] == 0
assert config [ " Description " ] == " test lambda function "
assert (
config [ " FunctionArn " ]
== f " arn:aws:lambda: { _lambda_region } : { ACCOUNT_ID } :function: { function_name } :2 "
)
assert config [ " FunctionName " ] == function_name
assert config [ " Version " ] == " 2 "
assert config [ " LastUpdateStatus " ] == " Successful "
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-07 17:11:13 +00:00
def test_create_function_with_invalid_arn ( ) :
err = create_invalid_lambda ( " test-iam-role " )
2023-06-11 18:44:30 +00:00
assert (
err . value . response [ " Error " ] [ " Message " ]
== r " 1 validation error detected: Value ' test-iam-role ' at ' role ' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:(aws[a-zA-Z-]*)?:iam::( \ d {12} ):role/?[a-zA-Z_0-9+=,.@ \ -_/]+ "
2019-11-07 17:11:13 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-07 17:11:13 +00:00
def test_create_function_with_arn_from_different_account ( ) :
err = create_invalid_lambda ( " arn:aws:iam::000000000000:role/example_role " )
2023-06-11 18:44:30 +00:00
assert (
err . value . response [ " Error " ] [ " Message " ]
== " Cross-account pass role is not allowed. "
2019-11-07 17:11:13 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2019-11-07 17:11:13 +00:00
def test_create_function_with_unknown_arn ( ) :
err = create_invalid_lambda (
" arn:aws:iam:: " + str ( ACCOUNT_ID ) + " :role/service-role/unknown_role "
)
2023-06-11 18:44:30 +00:00
assert (
err . value . response [ " Error " ] [ " Message " ]
== " The role defined for the function cannot be assumed by Lambda. "
2019-11-07 17:11:13 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2022-02-09 19:31:53 +00:00
def test_remove_unknown_permission_throws_error ( ) :
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
f = conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2022-02-09 19:31:53 +00:00
Role = ( get_role_name ( ) ) ,
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
)
arn = f [ " FunctionArn " ]
with pytest . raises ( ClientError ) as exc :
conn . remove_permission ( FunctionName = arn , StatementId = " 1 " )
err = exc . value . response [ " Error " ]
2023-06-11 18:44:30 +00:00
assert err [ " Code " ] == " ResourceNotFoundException "
assert err [ " Message " ] == " No policy is associated with the given resource. "
2022-10-18 09:35:17 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2022-10-18 09:35:17 +00:00
def test_multiple_qualifiers ( ) :
client = boto3 . client ( " lambda " , " us-east-1 " )
zip_content = get_test_zip_file1 ( )
fn_name = str ( uuid4 ( ) ) [ 0 : 6 ]
client . create_function (
FunctionName = fn_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2022-10-18 09:35:17 +00:00
Role = ( get_role_name ( ) ) ,
Handler = " lambda_function.handler " ,
Code = { " ZipFile " : zip_content } ,
)
for _ in range ( 10 ) :
2022-12-05 10:31:20 +00:00
new_zip = _process_lambda ( f " func content { _ } " )
client . update_function_code ( FunctionName = fn_name , ZipFile = new_zip )
2022-10-18 09:35:17 +00:00
client . publish_version ( FunctionName = fn_name )
resp = client . list_versions_by_function ( FunctionName = fn_name ) [ " Versions " ]
qualis = [ fn [ " FunctionArn " ] . split ( " : " ) [ - 1 ] for fn in resp ]
2023-06-11 18:44:30 +00:00
assert qualis == [ " $LATEST " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " 10 " ]
2022-10-18 09:35:17 +00:00
2023-11-10 19:13:13 +00:00
# Test delete with function name and qualifier
2022-10-18 09:35:17 +00:00
client . delete_function ( FunctionName = fn_name , Qualifier = " 4 " )
2023-11-10 19:13:13 +00:00
# Test delete with ARN and qualifier
client . delete_function (
FunctionName = f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { fn_name } " ,
Qualifier = " 5 " ,
)
# Test delete with qualifier part of function name
client . delete_function ( FunctionName = fn_name + " :8 " )
# Test delete with qualifier inside ARN
client . delete_function (
FunctionName = f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { fn_name } :9 "
)
2022-10-18 09:35:17 +00:00
resp = client . list_versions_by_function ( FunctionName = fn_name ) [ " Versions " ]
qualis = [ fn [ " FunctionArn " ] . split ( " : " ) [ - 1 ] for fn in resp ]
2023-11-10 19:13:13 +00:00
assert qualis == [ " $LATEST " , " 1 " , " 2 " , " 3 " , " 6 " , " 7 " , " 10 " ]
2022-10-18 09:35:17 +00:00
fn = client . get_function ( FunctionName = fn_name , Qualifier = " 6 " ) [ " Configuration " ]
2023-06-11 18:44:30 +00:00
assert (
fn [ " FunctionArn " ]
== f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function: { fn_name } :6 "
2022-10-18 09:35:17 +00:00
)
2022-11-07 20:03:07 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-11-10 19:13:13 +00:00
def test_delete_non_existent ( ) :
client = boto3 . client ( " lambda " , " us-east-1 " )
with pytest . raises ( ClientError ) as exc :
client . delete_function (
FunctionName = f " arn:aws:lambda:us-east-1: { ACCOUNT_ID } :function:nonexistent:9 "
)
assert exc . value . response [ " Error " ] [ " Code " ] == " ResourceNotFoundException "
2022-11-07 20:03:07 +00:00
def test_get_role_name_utility_race_condition ( ) :
# Play with these variables as needed to reproduce the error.
max_workers , num_threads = 3 , 15
errors = [ ]
roles = [ ]
def thread_function ( _ ) :
while True :
# noinspection PyBroadException
try :
role = get_role_name ( )
except ClientError as e :
errors . append ( str ( e ) )
break
except Exception :
# boto3 and our own IAMBackend are not thread-safe,
# and occasionally throw weird errors, so we just
# pass and retry.
# https://github.com/boto/boto3/issues/1592
pass
else :
roles . append ( role )
break
import concurrent . futures
with concurrent . futures . ThreadPoolExecutor ( max_workers = max_workers ) as executor :
executor . map ( thread_function , range ( num_threads ) )
# Check all threads are accounted for, all roles are the same entity,
# and there are no client errors.
assert len ( errors ) + len ( roles ) == num_threads
assert roles . count ( roles [ 0 ] ) == len ( roles )
assert len ( errors ) == 0
2023-08-15 16:43:40 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-15 16:43:40 +00:00
@mock.patch.dict ( os . environ , { " MOTO_LAMBDA_CONCURRENCY_QUOTA " : " 1000 " } )
def test_put_function_concurrency_success ( ) :
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2023-08-15 16:43:40 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 16:43:40 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
response = conn . put_function_concurrency (
FunctionName = function_name , ReservedConcurrentExecutions = 900
)
assert response [ " ReservedConcurrentExecutions " ] == 900
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-15 16:43:40 +00:00
@mock.patch.dict ( os . environ , { " MOTO_LAMBDA_CONCURRENCY_QUOTA " : " don ' t care " } )
def test_put_function_concurrency_not_enforced ( ) :
del os . environ [ " MOTO_LAMBDA_CONCURRENCY_QUOTA " ] # i.e. not set by user
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 16:43:40 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
# This works, even though it normally would be disallowed by AWS
response = conn . put_function_concurrency (
FunctionName = function_name , ReservedConcurrentExecutions = 901
)
assert response [ " ReservedConcurrentExecutions " ] == 901
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-15 16:43:40 +00:00
@mock.patch.dict ( os . environ , { " MOTO_LAMBDA_CONCURRENCY_QUOTA " : " 1000 " } )
def test_put_function_concurrency_failure ( ) :
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2023-08-15 16:43:40 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name = str ( uuid4 ( ) ) [ 0 : 6 ]
conn . create_function (
FunctionName = function_name ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 16:43:40 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
with pytest . raises ( ClientError ) as exc :
conn . put_function_concurrency (
FunctionName = function_name , ReservedConcurrentExecutions = 901
)
assert exc . value . response [ " Error " ] [ " Code " ] == " InvalidParameterValueException "
# No reservation should have been set
response = conn . get_function_concurrency ( FunctionName = function_name )
assert " ReservedConcurrentExecutions " not in response
2024-01-07 12:03:33 +00:00
@mock_aws
2023-08-15 16:43:40 +00:00
@mock.patch.dict ( os . environ , { " MOTO_LAMBDA_CONCURRENCY_QUOTA " : " 1000 " } )
def test_put_function_concurrency_i_can_has_math ( ) :
2023-09-19 19:46:20 +00:00
if not settings . TEST_DECORATOR_MODE :
2023-08-15 16:43:40 +00:00
raise SkipTest (
" Envars not easily set in server mode, feature off by default, skipping... "
)
conn = boto3 . client ( " lambda " , _lambda_region )
zip_content = get_test_zip_file1 ( )
function_name_1 = str ( uuid4 ( ) ) [ 0 : 6 ]
function_name_2 = str ( uuid4 ( ) ) [ 0 : 6 ]
conn . create_function (
FunctionName = function_name_1 ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 16:43:40 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
conn . create_function (
FunctionName = function_name_2 ,
2023-08-31 06:47:49 +00:00
Runtime = PYTHON_VERSION ,
2023-08-15 16:43:40 +00:00
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)
response = conn . put_function_concurrency (
FunctionName = function_name_1 , ReservedConcurrentExecutions = 600
)
assert response [ " ReservedConcurrentExecutions " ] == 600
response = conn . put_function_concurrency (
FunctionName = function_name_2 , ReservedConcurrentExecutions = 100
)
assert response [ " ReservedConcurrentExecutions " ] == 100
# Increasing function 1's limit should succeed, e.g. 700 + 100 <= 900
response = conn . put_function_concurrency (
FunctionName = function_name_1 , ReservedConcurrentExecutions = 700
)
assert response [ " ReservedConcurrentExecutions " ] == 700
# Increasing function 2's limit should fail, e.g. 700 + 201 > 900
with pytest . raises ( ClientError ) as exc :
conn . put_function_concurrency (
FunctionName = function_name_2 , ReservedConcurrentExecutions = 201
)
assert exc . value . response [ " Error " ] [ " Code " ] == " InvalidParameterValueException "
response = conn . get_function_concurrency ( FunctionName = function_name_2 )
assert response [ " ReservedConcurrentExecutions " ] == 100
2023-12-01 23:07:52 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
@pytest.mark.parametrize (
" config " ,
[
{
" OnSuccess " : {
" Destination " : f " arn:aws:lambda:us-west-2:123456789012:function: { LAMBDA_FUNC_NAME } -2 "
} ,
" OnFailure " : { } ,
} ,
{
" OnFailure " : {
" Destination " : f " arn:aws:lambda:us-west-2:123456789012:function: { LAMBDA_FUNC_NAME } -2 "
} ,
" OnSuccess " : { } ,
} ,
{
" OnFailure " : {
" Destination " : " arn:aws:lambda:us-west-2:123456789012:function:test-2 "
} ,
" OnSuccess " : {
" Destination " : " arn:aws:lambda:us-west-2:123456789012:function:test-2 "
} ,
} ,
] ,
)
def test_put_event_invoke_config ( config ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
# the name has to match ARNs in pytest parameterize
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
# Execute
result = client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Verify
assert result [ " FunctionArn " ] == arn_1
assert result [ " DestinationConfig " ] == config
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
@pytest.mark.parametrize (
" config " ,
[
{
" OnSuccess " : {
" Destination " : f " arn:aws:lambda:us-west-2:123456789012:function: { LAMBDA_FUNC_NAME } -2 "
} ,
" OnFailure " : { } ,
} ,
{
" OnFailure " : {
" Destination " : f " arn:aws:lambda:us-west-2:123456789012:function: { LAMBDA_FUNC_NAME } -2 "
} ,
" OnSuccess " : { } ,
} ,
{
" OnFailure " : {
" Destination " : " arn:aws:lambda:us-west-2:123456789012:function:test-2 "
} ,
" OnSuccess " : {
" Destination " : " arn:aws:lambda:us-west-2:123456789012:function:test-2 "
} ,
} ,
] ,
)
def test_update_event_invoke_config ( config ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
# the name has to match ARNs in pytest parameterize
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
# Execute
result = client . update_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Verify
assert result [ " FunctionArn " ] == arn_1
assert result [ " DestinationConfig " ] == config
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_put_event_invoke_config_errors_1 ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
config = {
" OnSuccess " : { " Destination " : " invalid " } ,
" OnFailure " : { } ,
}
# Execute
with pytest . raises ( ClientError ) as exc :
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Verify
err = exc . value . response [ " Error " ]
assert err [ " Code " ] == " ValidationException "
assert (
err [ " Message " ] == " 1 validation error detected: "
" Value ' invalid ' at ' destinationConfig.onSuccess.destination ' failed to satisfy constraint: "
" Member must satisfy regular expression pattern: "
r " ^$|arn:(aws[a-zA-Z0-9-]*):([a-zA-Z0-9 \ -])+:([a-z] {2} (-gov)?-[a-z]+- \ d {1} )?:( \ d {12} )?:(.*) "
)
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_put_event_invoke_config_errors_2 ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
config = {
" OnFailure " : { " Destination " : " invalid " } ,
" OnSuccess " : { } ,
}
# Execute
with pytest . raises ( ClientError ) as exc :
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Verify
err = exc . value . response [ " Error " ]
assert err [ " Code " ] == " ValidationException "
assert (
err [ " Message " ] == " 1 validation error detected: "
" Value ' invalid ' at ' destinationConfig.onFailure.destination ' failed to satisfy constraint: "
" Member must satisfy regular expression pattern: "
r " ^$|arn:(aws[a-zA-Z0-9-]*):([a-zA-Z0-9 \ -])+:([a-z] {2} (-gov)?-[a-z]+- \ d {1} )?:( \ d {12} )?:(.*) "
)
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_put_event_invoke_config_errors_3 ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
test_val = 5
# Execute
with pytest . raises ( ClientError ) as exc :
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , MaximumRetryAttempts = test_val
)
# Verify
err = exc . value . response [ " Error " ]
assert err [ " Code " ] == " ValidationException "
assert (
err [ " Message " ] == " 1 validation error detected: "
f " Value ' { test_val } ' at ' maximumRetryAttempts ' failed to satisfy constraint: "
" Member must have value less than or equal to 2 "
)
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_put_event_invoke_config_errors_4 ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
test_val = 44444
# Execute
with pytest . raises ( ClientError ) as exc :
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , MaximumEventAgeInSeconds = test_val
)
# Verify
err = exc . value . response [ " Error " ]
assert err [ " Code " ] == " ValidationException "
assert (
err [ " Message " ] == " 1 validation error detected: "
f " Value ' { test_val } ' at ' maximumEventAgeInSeconds ' failed to satisfy constraint: "
" Member must have value less than or equal to 21600 "
)
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_get_event_invoke_config ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
config = {
" OnFailure " : { " Destination " : arn_2 } ,
" OnSuccess " : { " Destination " : arn_2 } ,
}
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Execute
result = client . get_function_event_invoke_config ( FunctionName = LAMBDA_FUNC_NAME )
# Verify
assert result [ " DestinationConfig " ] == config
assert result [ " FunctionArn " ] == arn_1
assert " LastModified " in result
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_list_event_invoke_configs ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
config = {
" OnFailure " : { " Destination " : arn_2 } ,
" OnSuccess " : { " Destination " : arn_2 } ,
}
# Execute
result = client . list_function_event_invoke_configs ( FunctionName = LAMBDA_FUNC_NAME )
# Verify
assert " FunctionEventInvokeConfigs " in result
assert result [ " FunctionEventInvokeConfigs " ] == [ ]
# Execute
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
result = client . list_function_event_invoke_configs ( FunctionName = LAMBDA_FUNC_NAME )
# Verify
assert len ( result [ " FunctionEventInvokeConfigs " ] ) == 1
assert result [ " FunctionEventInvokeConfigs " ] [ 0 ] [ " DestinationConfig " ] == config
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_get_event_invoke_config_empty ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
# Execute
with pytest . raises ( ClientError ) as exc :
client . get_function_event_invoke_config ( FunctionName = LAMBDA_FUNC_NAME )
err = exc . value . response
# Verify
assert err [ " Error " ] [ " Code " ] == " ResourceNotFoundException "
assert (
err [ " Error " ] [ " Message " ]
== f " The function { arn_1 } doesn ' t have an EventInvokeConfig "
)
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
2024-01-07 12:03:33 +00:00
@mock_aws
2023-12-01 23:07:52 +00:00
def test_delete_event_invoke_config ( ) :
# Setup
client = boto3 . client ( " lambda " , _lambda_region )
arn_1 = setup_lambda ( client , LAMBDA_FUNC_NAME ) [ " FunctionArn " ]
# the name has to match ARNs in pytest parameterize
arn_2 = setup_lambda ( client , f " { LAMBDA_FUNC_NAME } -2 " ) [ " FunctionArn " ]
config = { " OnSuccess " : { " Destination " : arn_2 } , " OnFailure " : { } }
client . put_function_event_invoke_config (
FunctionName = LAMBDA_FUNC_NAME , DestinationConfig = config
)
# Execute
result = client . delete_function_event_invoke_config ( FunctionName = LAMBDA_FUNC_NAME )
# Verify
assert result [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 204
# Clean up for servertests
client . delete_function ( FunctionName = arn_1 )
client . delete_function ( FunctionName = arn_2 )
def setup_lambda ( client , name ) :
zip_content = get_test_zip_file1 ( )
return client . create_function (
FunctionName = name ,
Runtime = PYTHON_VERSION ,
Role = get_role_name ( ) ,
Handler = " lambda_function.lambda_handler " ,
Code = { " ZipFile " : zip_content } ,
Description = " test lambda function " ,
Timeout = 3 ,
MemorySize = 128 ,
Publish = True ,
)