Skip to content

Commit

Permalink
feat(api): Add support for X-Ray to AWS::Serverless::Api (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunedan authored and jlhood committed Nov 29, 2018
1 parent df32263 commit a35cadc
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ EndpointConfiguration All
MethodSettings All
BinaryMediaTypes All
Cors All
TracingEnabled All
================================== ======================== ========================


Expand Down
1 change: 1 addition & 0 deletions docs/globals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Currently, the following resources and properties are being supported:
Cors:
AccessLogSetting:
CanarySetting:
TracingEnabled:
SimpleTable:
# Properties of AWS::Serverless::SimpleTable
Expand Down
5 changes: 4 additions & 1 deletion samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class ApiGenerator(object):

def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variables, depends_on, definition_body, definition_uri, name, stage_name, endpoint_configuration=None, method_settings=None, binary_media=None, cors=None, auth=None, access_log_setting=None, canary_setting=None):
def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variables, depends_on, definition_body, definition_uri, name, stage_name, endpoint_configuration=None, method_settings=None, binary_media=None, cors=None, auth=None, access_log_setting=None, canary_setting=None, tracing_enabled=None):
"""Constructs an API Generator class that generates API Gateway resources
:param logical_id: Logical id of the SAM API Resource
Expand All @@ -37,6 +37,7 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
:param stage_name: Name of the Stage
:param access_log_setting: Whether to send access logs and where for Stage
:param canary_setting: Canary Setting for Stage
:param tracing_enabled: Whether active tracing with X-ray is enabled
"""
self.logical_id = logical_id
self.cache_cluster_enabled = cache_cluster_enabled
Expand All @@ -54,6 +55,7 @@ def __init__(self, logical_id, cache_cluster_enabled, cache_cluster_size, variab
self.auth = auth
self.access_log_setting = access_log_setting
self.canary_setting = canary_setting
self.tracing_enabled = tracing_enabled

def _construct_rest_api(self):
"""Constructs and returns the ApiGateway RestApi.
Expand Down Expand Up @@ -154,6 +156,7 @@ def _construct_stage(self, deployment, swagger):
stage.MethodSettings = self.method_settings
stage.AccessLogSetting = self.access_log_setting
stage.CanarySetting = self.canary_setting
stage.TracingEnabled = self.tracing_enabled

if swagger is not None:
deployment.make_auto_deployable(stage, swagger)
Expand Down
1 change: 1 addition & 0 deletions samtranslator/model/apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ApiGatewayStage(Resource):
'Description': PropertyType(False, is_str()),
'RestApiId': PropertyType(True, is_str()),
'StageName': PropertyType(True, one_of(is_str(), is_type(dict))),
'TracingEnabled': PropertyType(False, is_type(bool)),
'Variables': PropertyType(False, is_type(dict)),
"MethodSettings": PropertyType(False, is_type(list))
}
Expand Down
6 changes: 4 additions & 2 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ class SamApi(SamResourceMacro):
'Cors': PropertyType(False, one_of(is_str(), is_type(dict))),
'Auth': PropertyType(False, is_type(dict)),
'AccessLogSetting': PropertyType(False, is_type(dict)),
'CanarySetting': PropertyType(False, is_type(dict))
'CanarySetting': PropertyType(False, is_type(dict)),
'TracingEnabled': PropertyType(False, is_type(bool))
}

referable_properties = {
Expand Down Expand Up @@ -513,7 +514,8 @@ def to_cloudformation(self, **kwargs):
cors=self.Cors,
auth=self.Auth,
access_log_setting=self.AccessLogSetting,
canary_setting=self.CanarySetting)
canary_setting=self.CanarySetting,
tracing_enabled=self.TracingEnabled)

rest_api, deployment, stage, permissions = api_generator.to_cloudformation()

Expand Down
3 changes: 2 additions & 1 deletion samtranslator/plugins/globals/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class Globals(object):
"BinaryMediaTypes",
"Cors",
"AccessLogSetting",
"CanarySetting"
"CanarySetting",
"TracingEnabled"
],

SamResourceType.SimpleTable.value: [
Expand Down
3 changes: 3 additions & 0 deletions samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
}
]
},
"TracingEnabled": {
"type": "boolean"
},
"Variables": {
"additionalProperties": false,
"patternProperties": {
Expand Down
21 changes: 21 additions & 0 deletions tests/translator/input/api_with_xray_tracing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Resources:
HtmlFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.gethtml
Runtime: nodejs4.3
Events:
GetHtml:
Type: Api
Properties:
RestApiId: HtmlApi
Path: /
Method: get

HtmlApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionUri: s3://sam-demo-bucket/webpage_swagger.json
TracingEnabled: true
121 changes: 121 additions & 0 deletions tests/translator/output/api_with_xray_tracing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"Resources": {
"HtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}
}
},
"HtmlApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "HtmlApiDeploymentf117c932f7"
},
"RestApiId": {
"Ref": "HtmlApi"
},
"StageName": "Prod",
"TracingEnabled": true
}
},
"HtmlFunctionGetHtmlPermissionProd": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HtmlFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "Prod",
"__ApiId__": "HtmlApi"
}
]
}
}
},
"HtmlFunctionGetHtmlPermissionTest": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HtmlFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "*",
"__ApiId__": "HtmlApi"
}
]
}
}
},
"HtmlApiDeploymentf117c932f7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
"StageName": "Stage"
}
},
"HtmlApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"BodyS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "webpage_swagger.json"
}
}
},
"HtmlFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"HtmlFunctionRole",
"Arn"
]
},
"Runtime": "nodejs4.3",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
}
}
}
129 changes: 129 additions & 0 deletions tests/translator/output/aws-cn/api_with_xray_tracing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"Resources": {
"HtmlFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}
}
},
"HtmlApiProdStage": {
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "HtmlApiDeploymentf117c932f7"
},
"RestApiId": {
"Ref": "HtmlApi"
},
"StageName": "Prod",
"TracingEnabled": true
}
},
"HtmlFunctionGetHtmlPermissionProd": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HtmlFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "Prod",
"__ApiId__": "HtmlApi"
}
]
}
}
},
"HtmlFunctionGetHtmlPermissionTest": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "apigateway.amazonaws.com",
"FunctionName": {
"Ref": "HtmlFunction"
},
"SourceArn": {
"Fn::Sub": [
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
{
"__Stage__": "*",
"__ApiId__": "HtmlApi"
}
]
}
}
},
"HtmlApiDeploymentf117c932f7": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "HtmlApi"
},
"Description": "RestApi deployment id: f117c932f75cfa87d23dfed64e9430d0081ef289",
"StageName": "Stage"
}
},
"HtmlApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"EndpointConfiguration": {
"Types": [
"REGIONAL"
]
},
"BodyS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "webpage_swagger.json"
},
"Parameters": {
"endpointConfigurationTypes": "REGIONAL"
}
}
},
"HtmlFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"HtmlFunctionRole",
"Arn"
]
},
"Runtime": "nodejs4.3",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
}
}
}
Loading

0 comments on commit a35cadc

Please sign in to comment.