Skip to content

Commit

Permalink
feat: remove lambda_layers stack and bundle layers directly in stacks
Browse files Browse the repository at this point in the history
This is to avoid open bug: aws/aws-cdk#1972
  • Loading branch information
shaftoe committed Mar 22, 2020
1 parent f7e76ec commit 3b1a01e
Show file tree
Hide file tree
Showing 209 changed files with 19 additions and 65 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ bootstrap: requirements run-tests cdk_version

reminder:
@printf '$(GREEN)###########################################################################\n'
@printf '$(GREEN)# remember to add the DNS record to enable owned domain:\n#\n'
@printf '$(GREEN)# $(YELLOW)$(API_DOMAIN) $(GREEN)CNAME $(YELLOW)'
@printf '$(GREEN)remember to add $(YELLOW)$(API_DOMAIN)$(GREEN) CNAME DNS record to enable owned domain:\n'
@printf '$(YELLOW)'
@aws apigateway get-domain-name \
--profile $(AWS_PROFILE) \
--domain-name $(API_DOMAIN) \
| grep regionalDomainName \
| cut -d ':' -f 2 \
| tr -d ',' \
| tr -d '"'
--query "regionalDomainName" \
--output text
@printf '$(GREEN)###########################################################################$(CLR)\n'

synth: requirements run-tests clean cdk_version
Expand Down
11 changes: 0 additions & 11 deletions lib/cdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from aws_cdk import core
from stacks.api import ApiStack
from stacks.lambda_layers import LambdaLayersStack
from stacks.notifications import NotificationsStack
from stacks.publish_to_social import SocialPublishStack

Expand All @@ -26,15 +25,6 @@
### Main CDK code follows
APP = core.App()

LAYERS_STACK = LambdaLayersStack(
APP,
'lambda-layers',
tags={
'Managed': 'cdk',
'Name': 'lambda-layers',
},
)

NOTIFICATIONS_STACK = NotificationsStack(
APP,
'notifications',
Expand All @@ -47,7 +37,6 @@
PUBLISH_TO_SOCIAL_STACK = SocialPublishStack(
APP,
'publish-to-social',
lambda_layers=LAYERS_STACK.layers,
tags={
'Managed': 'cdk',
'Name': 'publish-to-social',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 0 additions & 38 deletions lib/stacks/lambda_layers/__init__.py

This file was deleted.

11 changes: 6 additions & 5 deletions lib/stacks/publish_to_social/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from os import environ
from typing import Iterable


from aws_cdk import (
aws_lambda,
Expand All @@ -12,6 +10,7 @@
)

from utils.cdk import (
get_layer,
get_lambda,
code_from_path,
DEFAULT_LOG_RETENTION,
Expand All @@ -20,8 +19,7 @@

class SocialPublishStack(core.Stack):

def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-builtin
lambda_layers: Iterable[aws_lambda.ILayerVersion], **kwargs) -> None:
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: # pylint: disable=redefined-builtin

super().__init__(scope, id, **kwargs)

Expand All @@ -32,6 +30,10 @@ def __init__(self, scope: core.Construct, id: str, # pylint: disable=redefined-

code = code_from_path(path='lib/stacks/{}/lambda'.format(id))

# Lambda Layers
lambda_layers = {layer: get_layer(self, layer_name=layer, prefix=id)
for layer in ("bs4", "requests_oauthlib")}

# PUBLISH lambda
lambda_publish_to_social = get_lambda(
self,
Expand Down Expand Up @@ -100,6 +102,5 @@ def build_lambda(name):

topic.add_subscription(aws_sns_subscriptions.LambdaSubscription(_lambda))


for social in social_lambdas:
build_lambda(name=social)
14 changes: 9 additions & 5 deletions lib/utils/cdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,29 @@ def get_lambda(scope: core.Construct, id: str, # pylint: disable=redefined-buil
)


def get_layer(scope: core.Construct, id: str, # pylint: disable=redefined-builtin,invalid-name
code_path: str,
def get_layer(scope: core.Construct,
layer_name: str,
prefix: str,
compatible_runtimes: Optional[Iterable[aws_lambda.Runtime]] = None,
description: Optional[str] = None) -> aws_lambda.LayerVersion:

if not compatible_runtimes:
compatible_runtimes = [DEFAULT_RUNTIME]

if not description:
description = f"Add {layer_name} dependency"

return aws_lambda.LayerVersion(
scope,
id,
code=code_from_path(path=code_path),
f"{prefix}-lambda-layer-python3-{layer_name}",
code=code_from_path(path=f"lib/layers/{layer_name}"),
compatible_runtimes=compatible_runtimes,
license="Apache-2.0",
description=description,
)


def get_bucket(scope: core.Construct, construct_id: str, # pylint: disable=redefined-builtin,invalid-name
def get_bucket(scope: core.Construct, construct_id: str,
expiration: Optional[core.Duration] = DEFAULT_S3_EXPIRATION) -> aws_s3.IBucket:
return aws_s3.Bucket(
scope,
Expand Down

0 comments on commit 3b1a01e

Please sign in to comment.