def test(self):
        lbd_permission = awslambda.Permission("LbdPermission",
                                              Action="",
                                              FunctionName="",
                                              Principal="")

        lbd_func = awslambda.Function(
            "LbdFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        api_method = apigateway.Method(
            "ApiMethod",
            Metadata={
                TROPOSPHERE_METADATA_FIELD_NAME: {
                    ResourceLevelField.ApiResource.FULL_PATH: "users"
                }
            },
            AuthorizationType="none",
            HttpMethod="POST",
            ResourceId="",
            RestApiId="",
        )

        associate(lbd_permission, api_method, lbd_func)
        assert lbd_permission.Action == "lambda:InvokeFunction"
        assert isinstance(lbd_permission.FunctionName, GetAtt)
        assert lbd_permission.Principal == "apigateway.amazonaws.com"
        assert isinstance(lbd_permission.SourceArn, Sub)
        assert len(lbd_permission.DependsOn) == 2
    def test(self):
        lbd_permission = awslambda.Permission("LbdPermission",
                                              Action="",
                                              FunctionName="",
                                              Principal="")

        lbd_func = awslambda.Function(
            "LbdFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        event_rule = events.Rule("EventRule")

        associate(lbd_permission, event_rule, lbd_func)

        assert lbd_permission.Action == "lambda:InvokeFunction"
        assert isinstance(lbd_permission.FunctionName, GetAtt)
        assert lbd_permission.Principal == "events.amazonaws.com"
        assert isinstance(lbd_permission.SourceArn, GetAtt)
        assert len(lbd_permission.DependsOn) == 2

        assert len(event_rule.Targets) == 1
        assert lbd_func.title in event_rule.Targets[0].Id
Example #3
0
 def test(self):
     lbd_func = awslambda.Function(
         title="MyFunc",
         Code=awslambda.Code(
             S3Bucket="my-bucket",
             S3Key="0.0.1.zip",
         ),
         Handler="my_func.handler",
         Role="arn:aws:iam::111122223333:role/todo",
         Runtime="python3.6")
     associate(lbd_func, kms_key)
     assert isinstance(lbd_func.KmsKeyArn, GetAtt)
Example #4
0
    def test(self):
        lbd_func = awslambda.Function(
            title="MyFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        assert isinstance(lbd_func.Role, str)
        associate(iam_role, lbd_func)  # intentionally do it in reverse order
        assert isinstance(lbd_func.Role, Ref)
Example #5
0
    def test(self):
        # subnet first then sg
        lbd_func = awslambda.Function(
            title="MyFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        associate(lbd_func, public_subnet1)
        associate(lbd_func, public_subnet2)
        associate(lbd_func, sg)

        assert len(lbd_func.VpcConfig.SubnetIds) == 2
        assert len(lbd_func.VpcConfig.SecurityGroupIds) == 1

        # sg first then subnet
        lbd_func = awslambda.Function(
            title="MyFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        associate(lbd_func, sg)
        associate(lbd_func, public_subnet1)
        associate(lbd_func, public_subnet2)

        assert len(lbd_func.VpcConfig.SubnetIds) == 2
        assert len(lbd_func.VpcConfig.SecurityGroupIds) == 1
    def test(self):
        lbd_permission = awslambda.Permission("LbdPermission",
                                              Action="",
                                              FunctionName="",
                                              Principal="")

        lbd_func = awslambda.Function(
            "LbdFunc",
            Code=awslambda.Code(
                S3Bucket="my-bucket",
                S3Key="0.0.1.zip",
            ),
            Handler="my_func.handler",
            Role="arn:aws:iam::111122223333:role/todo",
            Runtime="python3.6")

        authorizer = apigateway.Authorizer(
            "Authorizer",
            Name="MyAuthorizer",
            Type="",
            AuthorizerUri="",
            IdentitySource="",
        )

        rest_api = apigateway.RestApi("RestApi", )

        associate(rest_api,
                  authorizer,
                  lbd_func,
                  lbd_permission,
                  authorizer_type_is_token=True)

        assert lbd_permission.Action == "lambda:InvokeFunction"
        assert isinstance(lbd_permission.FunctionName, GetAtt)
        assert lbd_permission.Principal == "apigateway.amazonaws.com"
        assert isinstance(lbd_permission.SourceArn, Sub)
        assert len(lbd_permission.DependsOn) == 3

        assert authorizer.Type == "TOKEN"
        assert authorizer.IdentitySource == "method.request.header.auth"
        assert isinstance(authorizer.RestApiId, Ref)
        assert isinstance(authorizer.AuthorizerUri, Sub)
        assert len(authorizer.DependsOn) == 2
Example #7
0
 def test(self):
     api_method = apigateway.Method(
         "ApiMethod",
         AuthorizationType="none",
         HttpMethod="POST",
         ResourceId="",
         RestApiId="",
         Integration=apigateway.Integration(Type=""),
     )
     lbd_func = awslambda.Function(
         "LbdFunc",
         Code=awslambda.Code(
             S3Bucket="my-bucket",
             S3Key="0.0.1.zip",
         ),
         Handler="my_func.handler",
         Role="arn:aws:iam::111122223333:role/todo",
         Runtime="python3.6")
     associate(api_method, lbd_func)
     assert api_method.Integration.Type == "AWS"
     assert api_method.Integration.IntegrationHttpMethod == "POST"
     assert isinstance(api_method.Integration.Uri, Sub)
# -*- coding: utf-8 -*-

import pytest
from troposphere import Ref, GetAtt

from troposphere_mate import awslambda, sqs, kinesis, dynamodb
from troposphere_mate.associate import LinkerApi, associate

lbd_func = awslambda.Function(title="MyFunc",
                              Code=awslambda.Code(
                                  S3Bucket="my-bucket",
                                  S3Key="0.0.1.zip",
                              ),
                              Handler="my_func.handler",
                              Role="arn:aws:iam::111122223333:role/todo",
                              Runtime="python3.6")

sqs_queue = sqs.Queue(title="SQSQueue")

kinesis_stream = kinesis.Stream(title="KinesisStream", )

dynamodb_table = dynamodb.Table(
    title="DynamodbTable",
    AttributeDefinitions=[],
    KeySchema=[],
)


# from pprint import pprint
# pprint(LinkerApi._declared_linkers)
class TestLbdEventMapWithLbdFuncAndSQS(object):
                                            dynamodb.AttributeDefinition(
                                                AttributeName="id",
                                                AttributeType="S",
                                            ),
                                        ])

sqs_queue = sqs.Queue(
    "SQSQueueScheduler",
    template=template,
    QueueName=helper_fn_sub("{}-download-job", param_env_name),
    VisibilityTimeout=180,
)

#--- Lambda Function
aws_lambda_func_code = awslambda.Code(
    S3Bucket=config.S3_BUCKET_FOR_DEPLOY.get_value(),
    S3Key=config.LAMBDA_CODE_S3_KEY.get_value())
aws_lambda_layers = config.LAMBDA_LAYERS.get_value()
aws_lambda_environment = awslambda.Environment(Variables={
    k: v
    for k, v in config.to_dict(prefix="SEEDINVEST_MONITOR_").items()
    if isinstance(v, (str, int))
}, )

lbd_func_update_new_project = awslambda.Function(
    "LbdFuncUpdateNewProject",
    template=template,
    FunctionName=helper_fn_sub("{}-update-new-project", param_env_name),
    Code=aws_lambda_func_code,
    Layers=aws_lambda_layers,
    Handler="seedinvest_monitor.handlers.update_new_project.handler",
Example #10
0
from .app_config_init import app_config

template = tm.Template()

param_env_name = tm.Parameter(
    "EnvironmentName",
    Type="String",
)
template.add_parameter(param_env_name)

rest_api = apigateway.RestApi(
    "RestApi",
    template=template,
    Name=tm.helper_fn_sub("{}", param_env_name),
    EndpointConfiguration=apigateway.EndpointConfiguration(Types=[
        "REGIONAL",
    ]))

lambda_code = awslambda.Code(
    S3Bucket=app_config.LAMBDA_CODE_S3_BUCKET.get_value(),
    S3Key=app_config.LAMBDA_CODE_S3_KEY.get_value(),
)

iam_role = iam.Role(
    "IamRoleForLbdFunc",
    template=template,
    RoleName=tm.helper_fn_sub("{}-lbd-func", param_env_name),
    AssumeRolePolicyDocument=create_assume_role_policy_document(
        [AWSServiceName.aws_Lambda]),
    ManagedPolicyArns=[AWSManagedPolicyArn.awsLambdaBasicExecutionRole])