Esempio n. 1
0
 def test_api_no_definition(self):
     serverless_api = Api(
         "SomeApi",
         StageName="test",
     )
     t = Template()
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 2
0
 def test_required_api_definitionbody(self):
     serverless_api = Api(
         "SomeApi",
         StageName='test',
         DefinitionBody=self.swagger,
     )
     t = Template()
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 3
0
 def test_required_api_definitionuri(self):
     serverless_api = Api(
         "SomeApi",
         StageName='test',
         DefinitionUri='s3://bucket/swagger.yml',
     )
     t = Template()
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 4
0
 def test_api_with_endpoint_configuration(self):
     serverless_api = Api(
         title="SomeApi",
         StageName="testStageName",
         EndpointConfiguration=EndpointConfiguration(Type="PRIVATE"),
     )
     t = Template()
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 5
0
 def test_required_api_both(self):
     serverless_api = Api(
         "SomeApi",
         StageName='test',
         DefinitionUri='s3://bucket/swagger.yml',
         DefinitionBody=self.swagger,
     )
     t = Template()
     t.add_resource(serverless_api)
     with self.assertRaises(ValueError):
         t.to_json()
Esempio n. 6
0
 def test_api_auth_resource_policy(self):
     serverless_api = Api(
         title="SomeApi",
         Auth=Auth(ResourcePolicy=ResourcePolicyStatement(
             AwsAccountBlacklist=["testAwsAccountBlacklist"],
             AwsAccountWhitelist=["testAwsAccountWhitelist"],
             CustomStatements=["testCustomStatements"],
             IpRangeBlacklist=["testIpRangeBlacklist"],
             IpRangeWhitelist=["testIpRangeWhitelist"],
             SourceVpcBlacklist=["testVpcBlacklist"],
             SourceVpcWhitelist=["testVpcWhitelist"],
         ), ),
         StageName="testStageName",
     )
     t = Template()
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 7
0
 def test_api_with_domain(self):
     certificate = Parameter("certificate", Type="String")
     serverless_api = Api(
         "SomeApi",
         StageName="test",
         Domain=Domain(
             BasePath=["/"],
             CertificateArn=Ref(certificate),
             DomainName=Sub("subdomain.${Zone}",
                            Zone=ImportValue("MyZone")),
             EndpointConfiguration="REGIONAL",
             Route53=Route53(
                 HostedZoneId=ImportValue("MyZone"),
                 IpV6=True,
             ),
         ),
     )
     t = Template()
     t.add_parameter(certificate)
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 8
0
 def test_api_with_domain(self):
     certificate = Parameter('certificate', Type='String')
     serverless_api = Api(
         'SomeApi',
         StageName='test',
         Domain=Domain(
             BasePath=['/'],
             CertificateArn=Ref(certificate),
             DomainName=Sub('subdomain.${Zone}',
                            Zone=ImportValue('MyZone')),
             EndpointConfiguration='REGIONAL',
             Route53=Route53(
                 HostedZoneId=ImportValue('MyZone'),
                 IpV6=True,
             ),
         ),
     )
     t = Template()
     t.add_parameter(certificate)
     t.add_resource(serverless_api)
     t.to_json()
Esempio n. 9
0
def create_aurora_template(region, account_id):
    template = Template()
    template.set_transform('AWS::Serverless-2016-10-31')

    api_name = template.add_parameter(parameter=Parameter(
        title='ApiName',
        Default='sample-api',
        Type='String',
    ))

    function_name = template.add_parameter(parameter=Parameter(
        title='FunctionName',
        Default='sample-lambda-function',
        Type='String',
    ))

    # swagger_path = template.add_parameter(
    #     parameter=Parameter(
    #         title='SwaggerPath',
    #         Default='./swagger.yml',
    #         Type='String',
    #     )
    # )

    stage_name = template.add_parameter(parameter=Parameter(
        title='StageName',
        Default='prod',
        Type='String',
    ))

    api = template.add_resource(resource=Api(
        title='SampleApi',
        Name=Ref(api_name),
        # DefinitionUri=Ref(swagger_path),
        DefinitionUri='./swagger.yml',
        StageName=Ref(stage_name),
    ))

    path = '/sample/'
    method = 'get'
    function = template.add_resource(
        resource=Function(title='SampleLambdaFunction',
                          AutoPublishAlias='sample',
                          CodeUri='.',
                          FunctionName=Ref(function_name),
                          Handler='lambda_function.lambda_handler',
                          Role=ImportValue('sample-lambda-service-role-arn'),
                          Runtime='python3.7',
                          Events={
                              'ApiTrigger': {
                                  'Type': 'Api',
                                  'Properties': {
                                      'Path': path,
                                      'Method': method,
                                      'RestApiId': Ref(api)
                                  }
                              }
                          }))

    template.add_resource(
        resource=Permission(title='SampleLambdaFunctionPermission',
                            Action='lambda:InvokeFunction',
                            FunctionName=Ref(function),
                            Principal='apigateway.amazonaws.com'))

    with open('swagger_template.yml') as f:
        swagger_yaml = f.read()

    uri = URI.replace('{region}', region).replace('{account_id}', account_id) \
        .replace('{function_name}', function_name.Default)  # TODO:
    swagger = swagger_yaml.replace('{path}',
                                   path).replace('{method}',
                                                 method).replace('{uri}', uri)

    with open('./api.yml', mode='w') as file:
        file.write(template.to_yaml())

    with open('./swagger.yml', mode='w') as file:
        file.write(swagger)
Esempio n. 10
0
    LogGroupName=Join('/', ['/aws/lambda', Ref(consume_events_function)]),
    RetentionInDays=7,
))

template.add_resource(EventSourceMapping(
    'InvokeConsumeEvents',
    EventSourceArn=ImportValue(Join('-', [Ref(core_stack), 'EventsToDashboardQueue-Arn'])),
    FunctionName=Ref(consume_events_function),
    Enabled=True,
))

admin_api = template.add_resource(Api(
    'AdminApi',
    StageName='v1',
    EndpointConfiguration='REGIONAL',
    Cors=Cors(
        AllowHeaders="'*'",
        AllowMethods="'*'",
        AllowOrigin="'*'",
    ),
))

readonly_function_role = template.add_resource(Role(
    'ReadonlyVideoApiRole',
    Path="/",
    AssumeRolePolicyDocument={
        "Version": "2012-10-17",
        "Statement": [{
            "Action": ["sts:AssumeRole"],
            "Effect": "Allow",
            "Principal": {"Service": ["lambda.amazonaws.com", "apigateway.amazonaws.com"]},
        }],