8000 Test AWS::Lambda::Function code upload to s3 (#8632) · codeperl/localstack@a000683 · GitHub
[go: up one dir, main page]

Skip to content

Commit a000683

Browse files
Test AWS::Lambda::Function code upload to s3 (localstack#8632)
1 parent 2e81947 commit a000683

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

tests/integration/cloudformation/resources/test_lambda.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import base64
22
import json
33
import os
4+
from io import BytesIO
45

56
import pytest
67

7-
from localstack.aws.api.lambda_ import InvocationType, State
8+
from localstack.aws.api.lambda_ import InvocationType, Runtime, State
89
from localstack.testing.aws.lambda_utils import is_new_provider, is_old_provider
910
from localstack.testing.snapshots.transformer import SortingTransformer
1011
from localstack.utils.common import short_uid
12+
from localstack.utils.files import load_file
1113
from localstack.utils.http import safe_requests
1214
from localstack.utils.strings import to_bytes, to_str
1315
from localstack.utils.sync import retry, wait_until
14-
from localstack.utils.testutil import get_lambda_log_events
16+
from localstack.utils.testutil import create_lambda_archive, get_lambda_log_events
1517

1618
pytestmark = pytest.mark.skip_snapshot_verify(
1719
condition=is_old_provider,
@@ -1024,3 +1026,35 @@ def wait_for_logs():
10241026
# return len(events) >= 6 # note: each invoke comes with at least 3 events even without printing
10251027

10261028
wait_until(wait_for_logs)
1029+
1030+
1031+
@pytest.mark.aws_validated
1032+
def test_python_lambda_code_deployed_via_s3(deploy_cfn_template, aws_client, s3_bucket):
1033+
bucket_key = "handler.zip"
1034+
zip_file = create_lambda_archive(
1035+
load_file(
1036+
os.path.join(os.path.dirname(__file__), "../../awslambda/functions/lambda_echo.py")
1037+
),
1038+
get_content=True,
1039+
runtime=Runtime.python3_10,
1040+
)
1041+
aws_client.s3.upload_fileobj(BytesIO(zip_file), s3_bucket, bucket_key)
1042+
1043+
deployment = deploy_cfn_template(
1044+
template_path=os.path.join(
1045+
os.path.dirname(__file__), "../../templates/cfn_lambda_s3_code.yaml"
1046+
),
1047+
parameters={
1048+
"LambdaCodeBucket": s3_bucket,
1049+
"LambdaRuntime": "python3.10",
1050+
"LambdaHandler": "handler.handler",
1051+
},
1052+
)
1053+
1054+
function_name = deployment.outputs["LambdaName"]
1055+
invocation_result = aws_client.awslambda.invoke(
1056+
FunctionName=function_name, Payload=json.dumps({"hello": "world"})
1057+
)
1058+
payload = json.loads(to_str(invocation_result["Payload"].read()))
1059+
assert payload == {"hello": "world"}
1060+
assert invocation_result["StatusCode"] == 200
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Parameters:
2+
LambdaCodeBucket:
3+
Type: String
4+
LambdaRuntime:
5+
Type: String
6+
LambdaHandler:
7+
Type: String
8+
Resources:
9+
FunctionServiceRole675BB04A:
10+
Type: AWS::IAM::Role
11+
Properties:
12+
AssumeRolePolicyDocument:
13+
Statement:
14+
- Action: sts:AssumeRole
15+
Effect: Allow
16+
Principal:
17+
Service: lambda.amazonaws.com
18+
Version: "2012-10-17"
19+
ManagedPolicyArns:
20+
- Fn::Join:
21+
- ""
22+
- - "arn:"
23+
- Ref: AWS::Partition
24+
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
25+
Function76856677:
26+
Type: AWS::Lambda::Function
27+
Properties:
28+
Code:
29+
S3Bucket:
30+
Ref: LambdaCodeBucket
31+
S3Key: handler.zip
32+
Role:
33+
Fn::GetAtt:
34+
- FunctionServiceRole675BB04A
35+
- Arn
36+
Handler: !Ref LambdaHandler
37+
Runtime: !Ref LambdaRuntime
38+
DependsOn:
39+
- FunctionServiceRole675BB04A
40+
Outputs:
41+
LambdaName:
42+
Value:
43+
Ref: Function76856677
44+
LambdaArn:
45+
Value:
46+
Fn::GetAtt:
47+
- Function76856677
48+
- Arn

0 commit comments

Comments
 (0)
0