-
Notifications
You must be signed in to change notification settings - Fork 272
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
Python the-basic-mq #112
Merged
cdk-patterns
merged 2 commits into
cdk-patterns:master
from
6293:feature/the-basic-mq-py
Sep 2, 2020
Merged
Python the-basic-mq #112
Changes from all commits
Commits
Show all changes
2 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
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,10 @@ | ||
*.swp | ||
package-lock.json | ||
__pycache__ | ||
.pytest_cache | ||
.env | ||
*.egg-info | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out |
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,203 @@ | ||
# The Basic MQ | ||
|
||
![architecture](img/the-basic-mq-arch.png) | ||
|
||
This is an example cdk stack to deploy [static custom domain endpoints with Amazon MQ](https://aws.amazon.com/blogs/compute/creating-static-custom-domain-endpoints-with-amazon-mq/) from Rachel Richardson. | ||
|
||
In this example we have private Amazon MQ brokers behind an internet-facing network load balancer endpoint using a subdomain. | ||
|
||
## Pre Requirements | ||
|
||
This pattern requires you to have a Route53 Hosted Zone already in your account so that you can assign a public URL to your NLB. | ||
|
||
After you setup a hosted zone, you can get the id from the console and replace the value for 'hosted_zone_id' in the cdk stack. Then you need to replace 'zone_name' and 'subdomain_name' with the url you desire in that hosted zone. | ||
|
||
Also, you have to have a certificate for the subdomain before you deploy the stack (to avoid exceeding [ACM yearly certificate limit](https://github.com/aws/aws-cdk/issues/5889)). | ||
|
||
so if your hosted zone id is 1234 and you want your public url to be mq.cdkpatterns.com you would do: | ||
|
||
```python | ||
# Paste Hosted zone ID from Route53 console 'Hosted zone details' | ||
hosted_zone_id = '1234' | ||
|
||
# If zone_name = 'cdkexample.com' and subdomain_name = 'iot', you can connect to the broker by 'iot.cdkexample.com'. | ||
zone_name = 'cdkpatterns.com' | ||
subdomain_name = 'mq' | ||
|
||
# Request and issue a certificate for the subdomain (iot.cdkexample.com in this example) beforehand, and paste ARN. | ||
cert_arn = 'arn:aws:acm:us-east-1:2XXXXXX9:certificate/exxxx8-xxxx-xxxx-xxxx-fxxxxxxxx9' | ||
``` | ||
|
||
After you have replaced these values from a console you can do: | ||
|
||
```bash | ||
cdk deploy | ||
``` | ||
|
||
## Testing broker connectivity | ||
|
||
Once you deploy the stack, you can connect to the broker. | ||
This time we will use [Amazon MQ workshop](https://github.com/aws-samples/amazon-mq-workshop) client application code from re:Invent 2018 | ||
to simplify connectivity test. | ||
|
||
### Step 1. Create an environment in AWS Cloud9 | ||
|
||
Sign in to the AWS Cloud9 console and create an environment. You can leave settings as default. | ||
After AWS Cloud9 creates your environment, you should see a bash shell window for the environment. | ||
|
||
### Step 2. Set up client application | ||
|
||
In the bash shell, clone the repo "amazon-mq-workshop" by running the following command: | ||
|
||
``` | ||
git clone https://github.com/aws-samples/amazon-mq-workshop.git | ||
``` | ||
|
||
Now the code is located on `~/environment/amazon-mq-workshop`. Next, type the following command one by one. | ||
|
||
``` | ||
cd ~/environment/amazon-mq-workshop | ||
./setup.sh | ||
export temp_url="<failover url>" | ||
echo "url=\"$temp_url\"" >> ~/.bashrc; source ~/.bashrc | ||
``` | ||
|
||
By doing so you can tell the client application where to connect. | ||
Make sure you replace with `<failover url>` something like `"failover:(ssl://mq.example.com:61617)"` | ||
(the NLB endpoint subdomain you defined in CDK stack). | ||
|
||
### Step 3. Connect | ||
|
||
Run the producer and consumer clients in separate terminal windows. | ||
Run the following command to start the sender: | ||
|
||
``` | ||
java -jar ./bin/amazon-mq-client.jar -url $url -mode sender -type queue -destination workshop.queueA -name Sender-1 | ||
``` | ||
|
||
Open the other terminal and run the following command to start the receiver: | ||
|
||
``` | ||
java -jar ./bin/amazon-mq-client.jar -url $url -mode receiver -type queue -destination workshop.queueA | ||
``` | ||
|
||
If the messages are sent and received successfully across the internet, a log output should be | ||
|
||
(sender) | ||
|
||
``` | ||
ec2-user:~/environment/amazon-mq-workshop (master) $ java -jar ./bin/amazon-mq-client.jar -url $url -mode sender -type queue -destination workshop.queueA -name Sender-1 | ||
[ActiveMQ Task-1] INFO org.apache.activemq.transport.failover.FailoverTransport - Successfully connected to ssl://mq.example.com:61617 | ||
22.08.2020 01:17:53.958 - Sender: sent '[queue://workshop.queueA] [Sender-1] Message number 1' | ||
22.08.2020 01:17:54.975 - Sender: sent '[queue://workshop.queueA] [Sender-1] Message number 2' | ||
22.08.2020 01:17:55.990 - Sender: sent '[queue://workshop.queueA] [Sender-1] Message number 3' | ||
22.08.2020 01:17:57.8 - Sender: sent '[queue://workshop.queueA] [Sender-1] Message number 4' | ||
22.08.2020 01:17:58.27 - Sender: sent '[queue://workshop.queueA] [Sender-1] Message number 5' | ||
``` | ||
|
||
(receiver) | ||
|
||
``` | ||
ec2-user:~/environment/amazon-mq-workshop (master) $ java -jar ./bin/amazon-mq-client.jar -url $url -mode receiver -type queue -destination workshop.queueA | ||
[ActiveMQ Task-1] INFO org.apache.activemq.transport.failover.FailoverTransport - Successfully connected to ssl://mq.example.com:61617 | ||
22.08.2020 01:17:59.717 - Receiver: received '[queue://workshop.queueA] [Sender-1] Message number 1' | ||
22.08.2020 01:17:59.718 - Receiver: received '[queue://workshop.queueA] [Sender-1] Message number 2' | ||
22.08.2020 01:17:59.720 - Receiver: received '[queue://workshop.queueA] [Sender-1] Message number 3' | ||
22.08.2020 01:17:59.721 - Receiver: received '[queue://workshop.queueA] [Sender-1] Message number 4' | ||
22.08.2020 01:17:59.721 - Receiver: received '[queue://workshop.queueA] [Sender-1] Message number 5' | ||
``` | ||
|
||
That's it. You can also check [Lab 4: Testing a Broker Fail-over](https://github.com/aws-samples/amazon-mq-workshop/blob/master/labs/lab-4.md) | ||
to test this solution. | ||
|
||
## Logging into the broker’s ActiveMQ console from a browser | ||
|
||
Create a forwarding tunnel through an SSH connection to the bastion host. | ||
First, you need to add a rule allowing SSH connection from your computer, to the security group which the bastion host belongs to (bastionToMQGroup). | ||
You can retrieve bastionToMQGroup's security group ID and add the rule, and below is an example command in the terminal window. | ||
|
||
``` | ||
SGID=`aws cloudformation describe-stacks --stack-name TheBasicMQStack --region us-east-1 --output json | \ | ||
jq -r '.Stacks[0].Outputs[] | select (.OutputKey == "bastionToMQGroupSGID").OutputValue'` | ||
aws ec2 authorize-security-group-ingress --group-id ${SGID} --protocol tcp --port 22 --cidr YOUR-IP-ADDRESS/32 | ||
``` | ||
Next, push an SSH public key to the bastion host. The key is valid for 60 seconds. | ||
|
||
``` | ||
InstanceID=`aws cloudformation describe-stacks --stack-name TheBasicMQStack --region us-east-1 --output json | \ | ||
jq -r '.Stacks[0].Outputs[] | select (.OutputKey == "bastionInstanceID").OutputValue'` | ||
aws ec2-instance-connect send-ssh-public-key --instance-id ${InstanceID} --instance-os-user ec2-user --ssh-public-key 'file://~/.ssh/id_rsa.pub' --availability-zone us-east-1a | ||
``` | ||
|
||
Finally, create a forwarding tunnel through an SSH connection to the bastion host. | ||
|
||
``` | ||
InstancePublicDNS=`aws cloudformation describe-stacks --stack-name TheBasicMQStack --region us-east-1 --output json | \ | ||
jq -r '.Stacks[0].Outputs[] | select (.OutputKey == "bastionPublicDNS").OutputValue'` | ||
ssh -D 8162 -N -i ~/.ssh/id_rsa ec2-user@${InstancePublicDNS} | ||
``` | ||
|
||
Now you are ready to view broker’s ActiveMQ console from a browser. | ||
Open another window and run `aws mq describe-broker --broker-id myMQ` to get broker's endpoints. | ||
Note only one broker host is active at a time. | ||
|
||
If you use Firefox, go to Firefox Connection Settings. | ||
In the Configure Proxy Access to the Internet section, select Manual proxy configuration, | ||
then set the SOCKS Host to localhost and Port to 8162, leaving other fields empty. | ||
(See "Creating a forwarding tunnel" [here](https://aws.amazon.com/blogs/compute/creating-static-custom-domain-endpoints-with-amazon-mq/).) | ||
|
||
## Useful Commands | ||
|
||
The `cdk.json` file tells the CDK Toolkit how to execute your app. | ||
|
||
This project is set up like a standard Python project. The initialization | ||
process also creates a virtualenv within this project, stored under the .env | ||
directory. To create the virtualenv it assumes that there is a `python3` | ||
(or `python` for Windows) executable in your path with access to the `venv` | ||
package. If for any reason the automatic creation of the virtualenv fails, | ||
you can create the virtualenv manually. | ||
|
||
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 | ||
``` | ||
|
||
To add additional dependencies, for example other CDK libraries, just add | ||
them to your `setup.py` file and rerun the `pip install -r requirements.txt` | ||
command. | ||
|
||
## 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,11 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from aws_cdk import core | ||
|
||
from the_basic_mq.the_basic_mq_stack import TheBasicMQStack | ||
|
||
|
||
app = core.App() | ||
TheBasicMQStack(app, "TheBasicMQStack", env=core.Environment(region="us-east-1")) | ||
|
||
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,7 @@ | ||
{ | ||
"app": "python3 app.py", | ||
"context": { | ||
"@aws-cdk/core:enableStackNameDuplicates": "true", | ||
"aws-cdk:enableDiffNoFail": "true" | ||
} | ||
} |
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 @@ | ||
-e . |
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,45 @@ | ||
import setuptools | ||
|
||
|
||
with open("README.md") as fp: | ||
long_description = fp.read() | ||
|
||
|
||
setuptools.setup( | ||
name="python", | ||
version="0.0.1", | ||
|
||
description="An empty CDK Python app", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
|
||
author="author", | ||
|
||
package_dir={"": "python"}, | ||
packages=setuptools.find_packages(where="python"), | ||
|
||
install_requires=[ | ||
"aws-cdk.core==1.51.0", | ||
], | ||
|
||
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 |
Empty file.
Oops, something went wrong.
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.
The TS version creates the cert for you, what made you change it?
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.
With
DnsValidatedCertificate
CDK creates a different cert for the same domain when you delete and re-deploy the stack. You can issue 20 certs per year (aws/aws-cdk#5889) on new AWS accounts, and I thought it is better to manually issue a cert and use it by ARN.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.
Is it worth adding a comment to that effect? Linking to that issue
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.
I agree, I had better add some comment on it.