-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
feat: Passing Resources Between Stacks #210
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
# Passing References Between Stacks! | ||
|
||
These two folders contain examples of how to pass resources between stacks. | ||
|
||
In each folder, you can run the following commands to synthesize the CDK | ||
Applications. | ||
|
||
To manually create a virtualenv on MacOS and Linux: | ||
|
||
``` | ||
$ python3 -m venv .env | ||
``` | ||
|
||
After the init process completes and the virtualenv is created, you can use the following | ||
step to activate your virtualenv. | ||
|
||
``` | ||
$ source .env/bin/activate | ||
``` | ||
|
||
If you are a Windows platform, you would activate the virtualenv like this: | ||
|
||
``` | ||
% .env\Scripts\activate.bat | ||
``` | ||
|
||
Once the virtualenv is activated, you can install the required dependencies. | ||
|
||
``` | ||
$ pip install -r requirements.txt | ||
``` | ||
|
||
At this point you can now synthesize the CloudFormation template for this code. | ||
|
||
``` | ||
$ cdk synth | ||
``` | ||
|
||
You can now begin exploring the source code, contained in the hello directory. | ||
There is also a very trivial test included that can be run like this: | ||
|
||
``` | ||
$ pytest | ||
``` | ||
|
||
To add additional dependencies, for example other CDK libraries, just add to | ||
your requirements.txt file and rerun the `pip install -r requirements.txt` | ||
command. | ||
|
||
## Useful commands | ||
|
||
* `cdk ls` list all stacks in the app | ||
* `cdk synth` emits the synthesized CloudFormation template | ||
* `cdk deploy` deploy this stack to your default AWS account/region | ||
* `cdk diff` compare deployed stack with current state | ||
* `cdk docs` open CDK documentation | ||
|
||
Enjoy! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
# Native Objects Example For Passing Resources Between Stacks! | ||
|
||
One of my favorite parts about CDK is that I don't have to be concerned with how | ||
the underlying cloudformation expects resources. Ideally the level of | ||
abstraction that I want to deal with is that I have a Lambda Function in one | ||
stack and I want to just hand the whole function to other stacks that need it. | ||
I do not have to worry about whether API Gateway expects a Function Name or a | ||
Function ARN or some other identifier for a Function, I can simply pass the | ||
Function Object to the stacks that need it and let CDK handle the details. | ||
|
||
In this example we create an AWS Lambda Function in one stack and then make it | ||
available as a property of that stack for other stacks to consume. | ||
|
||
in infrastructure_stack.py: | ||
```python | ||
@property | ||
def main_function(self) -> lambda_.IFunction: | ||
return self._function | ||
``` | ||
|
||
We later refrence this property in app.py as follows: | ||
```python | ||
infra.main_function | ||
``` | ||
where `infra` is the variable we use to identify our InfrastructureStack. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from aws_cdk import core | ||
|
||
from native_objects.application_stack import ApplicationStack | ||
from native_objects.infrastructure_stack import InfrastructureStack | ||
|
||
|
||
app = core.App() | ||
|
||
env={'region': 'us-west-2'} | ||
# Base infrastructure stack, Lambda Functions, DynamoDB Tables, etc.... | ||
infra = InfrastructureStack(app, "infrastructure", env=env) | ||
|
||
# Application stack that generally changes independently of the underlying infrastructure stack | ||
application = ApplicationStack(app, "application", referenced_function=infra.main_function, env=env) | ||
|
||
app.synth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "python3 app.py" | ||
} |
Empty file.
13 changes: 13 additions & 0 deletions
13
python/cross-stack-resources/native-objects/native_objects/application_stack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from aws_cdk import ( | ||
aws_lambda as lambda_, | ||
aws_apigateway as apigw, | ||
core | ||
) | ||
|
||
class ApplicationStack(core.Stack): | ||
|
||
def __init__(self, scope: core.Construct, id: str, referenced_function: lambda_.IFunction, **kwargs) -> None: | ||
super().__init__(scope, id, **kwargs) | ||
|
||
my_api = apigw.LambdaRestApi(self, "myRestAPI", handler=referenced_function) | ||
|
25 changes: 25 additions & 0 deletions
25
python/cross-stack-resources/native-objects/native_objects/infrastructure_stack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from aws_cdk import ( | ||
aws_lambda as lambda_, | ||
core | ||
) | ||
|
||
class InfrastructureStack(core.Stack): | ||
|
||
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: | ||
super().__init__(scope, id, **kwargs) | ||
|
||
my_main_func = lambda_.Function( | ||
self, | ||
"myMainFunction", | ||
code=lambda_.InlineCode("def main(event, context)\n print('hello, world')"), | ||
handler='index.main', | ||
runtime=lambda_.Runtime.PYTHON_3_7 | ||
) | ||
|
||
# We assign the function to a local variable for the Object. | ||
self._function = my_main_func | ||
|
||
# Using the property decorator | ||
@property | ||
def main_function(self) -> lambda_.IFunction: | ||
return self._function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-e . | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import setuptools | ||
|
||
|
||
with open("README.md") as fp: | ||
long_description = fp.read() | ||
|
||
|
||
setuptools.setup( | ||
name="native-objects", | ||
version="1.0.0", | ||
|
||
description="A python CDK example for passing resources between stacks", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
|
||
author="Richard Boyd", | ||
|
||
package_dir={"": "native_objects"}, | ||
packages=setuptools.find_packages(where="native_objects"), | ||
|
||
install_requires=[ | ||
"aws-cdk.core", | ||
"aws-cdk.aws_apigateway", | ||
"aws-cdk.aws_lambda" | ||
], | ||
|
||
python_requires=">=3.6", | ||
|
||
classifiers=[ | ||
"Development Status :: 4 - Beta", | ||
|
||
"Intended Audience :: Developers", | ||
|
||
"License :: OSI Approved :: Apache Software License", | ||
|
||
"Programming Language :: JavaScript", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Programming Language :: Python :: 3.6", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
|
||
"Topic :: Software Development :: Code Generators", | ||
"Topic :: Utilities", | ||
|
||
"Typing :: Typed", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@echo off | ||
|
||
rem The sole purpose of this script is to make the command | ||
rem | ||
rem source .env/bin/activate | ||
rem | ||
rem (which activates a Python virtualenv on Linux or Mac OS X) work on Windows. | ||
rem On Windows, this command just runs this batch file (the argument is ignored). | ||
rem | ||
rem Now we don't need to document a Windows command for activating a virtualenv. | ||
|
||
echo Executing .env\Scripts\activate.bat for you | ||
.env\Scripts\activate.bat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
# Raw String Example For Passing Resources Between Stacks! | ||
|
||
If you're coming from a strong background in cloudformation, you may either | ||
have a preference for passing resources as strings (the way AWS Cloudformation | ||
does natively) or your existing infrastructure may already extensively leverage | ||
imports/exports and you'd like to maintain a consistent paradigm across your | ||
applications. | ||
|
||
In this example we create an AWS Lambda Function in one stack and then make its | ||
ARN available as a property of that stack for other stacks to consume. | ||
|
||
in infrastructure_stack.py: | ||
```python | ||
@property | ||
def main_function_arn(self): | ||
return self._function_arn | ||
``` | ||
|
||
We later refrence this property in app.py as follows: | ||
```python | ||
infra.main_function_arn | ||
``` | ||
where `infra` is the variable we use to identify our InfrastructureStack. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from aws_cdk import core | ||
|
||
from raw_strings.application_stack import ApplicationStack | ||
from raw_strings.infrastructure_stack import InfrastructureStack | ||
|
||
|
||
app = core.App() | ||
|
||
env={'region': 'us-west-2'} | ||
# Base infrastructure stack, Lambda Functions, DynamoDB Tables, etc.... | ||
infra = InfrastructureStack(app, "infrastructure", env=env) | ||
|
||
# Application stack that generally changes independently of the underlying infrastructure stack | ||
application = ApplicationStack(app, "application", lambda_arn=infra.main_function_arn, env=env) | ||
|
||
app.synth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"app": "python3 app.py" | ||
} |
Empty file.
14 changes: 14 additions & 0 deletions
14
python/cross-stack-resources/raw-strings/raw_strings/application_stack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from aws_cdk import ( | ||
aws_lambda as lambda_, | ||
aws_apigateway as apigw, | ||
core | ||
) | ||
|
||
class ApplicationStack(core.Stack): | ||
|
||
def __init__(self, scope: core.Construct, id: str, lambda_arn: str, **kwargs) -> None: | ||
super().__init__(scope, id, **kwargs) | ||
|
||
referenced_function = lambda_.Function.from_function_arn(self, id="LocalNameForFunction", function_arn=lambda_arn) | ||
my_api = apigw.LambdaRestApi(self, "myRestAPI", handler=referenced_function) | ||
|
26 changes: 26 additions & 0 deletions
26
python/cross-stack-resources/raw-strings/raw_strings/infrastructure_stack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from aws_cdk import ( | ||
aws_lambda as lambda_, | ||
aws_dynamodb as ddb, | ||
core | ||
) | ||
|
||
class InfrastructureStack(core.Stack): | ||
|
||
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: | ||
super().__init__(scope, id, **kwargs) | ||
|
||
my_main_func = lambda_.Function( | ||
self, | ||
"myMainFunction", | ||
code=lambda_.InlineCode("def main(event, context)\n print('hello, world')"), | ||
handler='index.main', | ||
runtime=lambda_.Runtime.PYTHON_3_7 | ||
) | ||
|
||
# We assign the function's arn to a local variable for the Object. | ||
self._function_arn = my_main_func.function_arn | ||
|
||
# Using the property decorator | ||
@property | ||
def main_function_arn(self): | ||
return self._function_arn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-e . | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import setuptools | ||
|
||
|
||
with open("README.md") as fp: | ||
long_description = fp.read() | ||
|
||
|
||
setuptools.setup( | ||
name="raw_strings", | ||
version="0.0.1", | ||
|
||
description="A sample CDK Python app", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
|
||
author="author", | ||
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. Set name and description |
||
|
||
package_dir={"": "raw_strings"}, | ||
packages=setuptools.find_packages(where="raw_strings"), | ||
|
||
install_requires=[ | ||
"aws-cdk.core", | ||
"aws-cdk.aws_apigateway", | ||
"aws-cdk.aws_lambda", | ||
"aws-cdk.aws_dynamodb" | ||
], | ||
|
||
python_requires=">=3.6", | ||
|
||
classifiers=[ | ||
"Development Status :: 4 - Beta", | ||
|
||
"Intended Audience :: Developers", | ||
|
||
"License :: OSI Approved :: Apache Software License", | ||
|
||
"Programming Language :: JavaScript", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Programming Language :: Python :: 3.6", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
|
||
"Topic :: Software Development :: Code Generators", | ||
"Topic :: Utilities", | ||
|
||
"Typing :: Typed", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@echo off | ||
|
||
rem The sole purpose of this script is to make the command | ||
rem | ||
rem source .env/bin/activate | ||
rem | ||
rem (which activates a Python virtualenv on Linux or Mac OS X) work on Windows. | ||
rem On Windows, this command just runs this batch file (the argument is ignored). | ||
rem | ||
rem Now we don't need to document a Windows command for activating a virtualenv. | ||
|
||
echo Executing .env\Scripts\activate.bat for you | ||
.env\Scripts\activate.bat |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Set version to 1.0.0