A python CDK App to provision an EC2 instance with Apache installed on it
- Python3
- AWS CLI
- NodeJs
- AWS CDK
- I've Went with CDK although the choices were Terraform or Cloudformation since CDK practically deploys a cloudformation stack and a template can be easily sythesized from the code
- I have not spent time writing code to deploy a dedicated VPC since a best practices VPC done by AWS is readily available and can be easily deployed into any account
- By design server can only be accessed using Session manager, hence no ssh key has been configured on the instance.
- Since not much detail was given, I've configured a really simple html page to replace the default page of Apache, otherwise there should be a virtual host configured
- Since the Instance is in a private subnet with no LB configured then it can be only tested from within the instance itself as illustrated below
[root@ip-10-10-22-169 ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
Welcome to B!
</head>
<body>
Hello World!
</body>
</html>
- Enable Encryption on the EBS volume for the instance
- Enable Encryption on s3 Bucket
- Parametrize Log group name and other variables in the cloudwatch agent json config file
- Build a Golden AMI with packer, version it and just lookup the AMI-ID with CDK and deploy it
- Configure an Autoscaling Group with a LB in front
To run CDK commands you can follow the default CDK guide at the end of this section or simply run Make
commands which have been programmed in the Makefile to create a python virtual environment, install requirements and then run the CDK command for you such as:
make synth
make test
make deploy
You can run make help
to get more info on the different commands
> make help
Usage:
make <target>
Targets:
test Run Unit tests on CDK code
synth Synthesize Cloudformation Stacks from CDK Code for the target account/environment
diff Show CDK diff on the target account/environment
context Fetch CDK context from the target account
deploy Deploy All CDK Stacks into the target account
destroy Destroy All CDK Stacks in the target account
bootstrap Install Bootstrap CDK into the target account
install-pre-commit Install Pre-Commit Hooks
Help
help Show help
This is a blank project for CDK development with Python.
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 .venv
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 .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
If you are a Windows platform, you would activate the virtualenv like this:
% .venv\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.
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
Enjoy!