8000 Add basic invoke test for AWS::Lambda::Function with code package on S3 by dominikschubert · Pull Request #8632 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Add basic invoke test for AWS::Lambda::Function with code package on S3 #8632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test AWS::Lambda::Function code upload to s3
  • Loading branch information
dominikschubert committed Jul 5, 2023
commit e8e7aa02033d09774ff87ba4f242154ad57e3d89
38 changes: 36 additions & 2 deletions tests/integration/cloudformation/resources/test_lambda.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import base64
import json
import os
from io import BytesIO

import pytest

from localstack.aws.api.lambda_ import InvocationType, State
from localstack.aws.api.lambda_ import InvocationType, Runtime, State
from localstack.testing.aws.lambda_utils import is_new_provider, is_old_provider
from localstack.testing.snapshots.transformer import SortingTransformer
from localstack.utils.common import short_uid
from localstack.utils.files import load_file
from localstack.utils.http import safe_requests
from localstack.utils.strings import to_bytes, to_str
from localstack.utils.sync import retry, wait_until
from localstack.utils.testutil import get_lambda_log_events
from localstack.utils.testutil import create_lambda_archive, get_lambda_log_events

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

wait_until(wait_for_logs)


@pytest.mark.aws_validated
def test_python_lambda_code_deployed_via_s3(deploy_cfn_template, aws_client, s3_bucket):
bucket_key = "handler.zip"
zip_file = create_lambda_archive(
load_file(
os.path.join(os.path.dirname(__file__), "../../awslambda/functions/lambda_echo.py")
),
get_content=True,
runtime=Runtime.python3_10,
)
aws_client.s3.upload_fileobj(BytesIO(zip_file), s3_bucket, bucket_key)

deployment = deploy_cfn_template(
template_path=os.path.join(
os.path.dirname(__file__), "../../templates/cfn_lambda_s3_code.yaml"
),
parameters={
"LambdaCodeBucket": s3_bucket,
"LambdaRuntime": "python3.10",
"LambdaHandler": "handler.handler",
},
)

function_name = deployment.outputs["LambdaName"]
invocation_result = aws_client.awslambda.invoke(
FunctionName=function_name, Payload=json.dumps({"hello": "world"})
)
payload = json.loads(to_str(invocation_result["Payload"].read()))
assert payload == {"hello": "world"}
assert invocation_result["StatusCode"] == 200
48 changes: 48 additions & 0 deletions tests/integration/templates/cfn_lambda_s3_code.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Parameters:
LambdaCodeBucket:
Type: String
LambdaRuntime:
Type: String
LambdaHandler:
Type: String
Resources:
FunctionServiceRole675BB04A:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Function76856677:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket:
Ref: LambdaCodeBucket
S3Key: handler.zip
Role:
Fn::GetAtt:
- FunctionServiceRole675BB04A
- Arn
Handler: !Ref LambdaHandler
Runtime: !Ref LambdaRuntime
DependsOn:
- FunctionServiceRole675BB04A
Outputs:
LambdaName:
Value:
Ref: Function76856677
LambdaArn:
Value:
Fn::GetAtt:
- Function76856677
- Arn
0