-
Notifications
You must be signed in to change notification settings - Fork 2.5k
add integration test for LMI functions #3863
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from unittest.case import skipIf | ||
|
|
||
| import pytest | ||
|
|
||
| from integration.config.service_names import LAMBDA_MANAGED_INSTANCES | ||
| from integration.helpers.base_test import BaseTest | ||
| from integration.helpers.resource import current_region_does_not_support | ||
|
|
||
|
|
||
| @skipIf( | ||
| current_region_does_not_support([LAMBDA_MANAGED_INSTANCES]), | ||
| "LambdaManagedInstance is not supported in this testing region", | ||
| ) | ||
| class TestFunctionWithCapacityProvider(BaseTest): | ||
| @pytest.fixture(autouse=True) | ||
| def companion_stack_outputs(self, get_companion_stack_outputs): | ||
| self.companion_stack_outputs = get_companion_stack_outputs | ||
|
|
||
| def test_function_with_capacity_provider_custom_role(self): | ||
| """Test Lambda function with CapacityProviderConfig using custom operator role.""" | ||
| # Phase 1: Prepare parameters from companion stack | ||
| parameters = [ | ||
| self.generate_parameter("SubnetId", self.companion_stack_outputs["LMISubnetId"]), | ||
| self.generate_parameter("SecurityGroup", self.companion_stack_outputs["LMISecurityGroupId"]), | ||
| self.generate_parameter("KMSKeyArn", self.companion_stack_outputs["LMIKMSKeyArn"]), | ||
| ] | ||
|
|
||
| # Phase 2: Deploy and verify against expected JSON | ||
| self.create_and_verify_stack("combination/function_lmi_custom", parameters) | ||
|
|
||
| # Phase 3: Verify resource counts | ||
| lambda_resources = self.get_stack_resources("AWS::Lambda::Function") | ||
| self.assertEqual(len(lambda_resources), 1, "Should create exactly one Lambda function") | ||
|
|
||
| capacity_provider_resources = self.get_stack_resources("AWS::Lambda::CapacityProvider") | ||
| self.assertEqual(len(capacity_provider_resources), 1, "Should create exactly one CapacityProvider") | ||
|
|
||
| iam_role_resources = self.get_stack_resources("AWS::IAM::Role") | ||
| self.assertEqual(len(iam_role_resources), 2, "Should create exactly two IAM roles") | ||
|
|
||
| def test_function_with_capacity_provider_default_role(self): | ||
| """Test Lambda function with CapacityProviderConfig using default operator role. | ||
|
|
||
| Note: This test is skipped until 12/01/2024 because the managed policy required for | ||
| the default operator role is not available until then. | ||
| """ | ||
| # Phase 1: Prepare parameters from companion stack | ||
| parameters = [ | ||
| self.generate_parameter("SubnetId", self.companion_stack_outputs["LMISubnetId"]), | ||
| self.generate_parameter("SecurityGroup", self.companion_stack_outputs["LMISecurityGroupId"]), | ||
| self.generate_parameter("KMSKeyArn", self.companion_stack_outputs["LMIKMSKeyArn"]), | ||
| ] | ||
|
|
||
| # Phase 2: Deploy and verify against expected JSON | ||
| self.create_and_verify_stack("combination/function_lmi_default", parameters) | ||
|
|
||
| # Phase 3: Verify resource counts | ||
| lambda_resources = self.get_stack_resources("AWS::Lambda::Function") | ||
| self.assertEqual(len(lambda_resources), 1, "Should create exactly one Lambda function") | ||
|
|
||
| capacity_provider_resources = self.get_stack_resources("AWS::Lambda::CapacityProvider") | ||
| self.assertEqual(len(capacity_provider_resources), 1, "Should create exactly one CapacityProvider") | ||
|
|
||
| iam_role_resources = self.get_stack_resources("AWS::IAM::Role") | ||
| self.assertEqual(len(iam_role_resources), 2, "Should create exactly two IAM roles") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [ | ||
| { | ||
| "LogicalResourceId": "MyCapacityProvider", | ||
| "ResourceType": "AWS::Lambda::CapacityProvider" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyCapacityProviderCustomRole", | ||
| "ResourceType": "AWS::IAM::Role" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyFunction", | ||
| "ResourceType": "AWS::Lambda::Function" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyFunctionRole", | ||
| "ResourceType": "AWS::IAM::Role" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [ | ||
| { | ||
| "LogicalResourceId": "MyCapacityProvider", | ||
| "ResourceType": "AWS::Lambda::CapacityProvider" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyCapacityProviderOperatorRole", | ||
| "ResourceType": "AWS::IAM::Role" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyFunction", | ||
| "ResourceType": "AWS::Lambda::Function" | ||
| }, | ||
| { | ||
| "LogicalResourceId": "MyFunctionRole", | ||
| "ResourceType": "AWS::IAM::Role" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| Parameters: | ||
| SubnetId: | ||
| Type: String | ||
| SecurityGroup: | ||
| Type: String | ||
| KMSKeyArn: | ||
| Type: String | ||
|
|
||
| Resources: | ||
| MyCapacityProviderCustomRole: | ||
| Type: AWS::IAM::Role | ||
| Properties: | ||
| AssumeRolePolicyDocument: | ||
| Version: '2012-10-17' | ||
| Statement: | ||
| - Effect: Allow | ||
| Principal: | ||
| Service: lambda.amazonaws.com | ||
| Action: sts:AssumeRole | ||
| Policies: | ||
| - PolicyName: CapacityProviderOperatorRolePolicy | ||
| PolicyDocument: | ||
| Version: '2012-10-17' | ||
| Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - ec2:TerminateInstances | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Operator role doesn't need |
||
| - ec2:AttachNetworkInterface | ||
| - ec2:RunInstances | ||
| - ec2:CreateTags | ||
| Resource: | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:instance/* | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:network-interface/* | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:volume/* | ||
| Condition: | ||
| StringEquals: | ||
| ec2:ManagedResourceOperator: scaler.lambda.amazonaws.com | ||
| - Effect: Allow | ||
| Action: | ||
| - ec2:DescribeAvailabilityZones | ||
| - ec2:DescribeCapacityReservations | ||
| - ec2:DescribeInstanceTypes | ||
| - ec2:DescribeInstanceTypeOfferings | ||
| - ec2:DescribeSecurityGroups | ||
| - ec2:DescribeSubnets | ||
| - ec2:DescribeInstances | ||
| - ec2:DescribeInstanceStatus | ||
|
Comment on lines
+40
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to not have these in alphabetical order? ( |
||
| Resource: '*' | ||
| - Effect: Allow | ||
| Action: | ||
| - ec2:RunInstances | ||
| - ec2:CreateNetworkInterface | ||
| Resource: | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:subnet/* | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:security-group/* | ||
| - Effect: Allow | ||
| Action: | ||
| - ec2:RunInstances | ||
| Resource: | ||
| - !Sub arn:${AWS::Partition}:ec2:*:*:image/* | ||
| Condition: | ||
| Bool: | ||
| ec2:Public: 'true' | ||
|
|
||
| MyCapacityProvider: | ||
| Type: AWS::Serverless::CapacityProvider | ||
| Properties: | ||
| CapacityProviderName: !Sub "${AWS::StackName}-cp" | ||
| VpcConfig: | ||
| SubnetIds: | ||
| - !Ref SubnetId | ||
| SecurityGroupIds: | ||
| - !Ref SecurityGroup | ||
| OperatorRole: !GetAtt MyCapacityProviderCustomRole.Arn | ||
| KmsKeyArn: !Ref KMSKeyArn | ||
|
|
||
| MyFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Runtime: nodejs22.x | ||
| Handler: index.handler | ||
| CodeUri: ${codeuri} | ||
| CapacityProviderConfig: | ||
| Arn: !GetAtt MyCapacityProvider.Arn | ||
|
|
||
| Metadata: | ||
| SamTransformTest: true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| Parameters: | ||
| SubnetId: | ||
| Type: String | ||
| SecurityGroup: | ||
| Type: String | ||
| KMSKeyArn: | ||
| Type: String | ||
|
|
||
| Resources: | ||
| MyCapacityProvider: | ||
| Type: AWS::Serverless::CapacityProvider | ||
| Properties: | ||
| CapacityProviderName: !Sub "${AWS::StackName}-cp" | ||
| VpcConfig: | ||
| SubnetIds: | ||
| - !Ref SubnetId | ||
| SecurityGroupIds: | ||
| - !Ref SecurityGroup | ||
| KmsKeyArn: !Ref KMSKeyArn | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have a Capacity provider without KmsKeyArn too, since it's not needed?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also without |
||
|
|
||
| MyFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| Runtime: nodejs22.x | ||
| Handler: index.handler | ||
| CodeUri: ${codeuri} | ||
| CapacityProviderConfig: | ||
| Arn: !GetAtt MyCapacityProvider.Arn | ||
|
|
||
| Metadata: | ||
| SamTransformTest: true | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this note (also: 2024???)