Skip to content

Commit e5a6aa6

Browse files
authored
Merge pull request #47 from open-mpic/birgelee-remote-perspective-code-from-call
get remote perspective name from call
2 parents 86b1100 + f571f5c commit e5a6aa6

File tree

19 files changed

+369
-320
lines changed

19 files changed

+369
-320
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ All requirements for running the API are packaged and uploaded to AWS as a lambd
1313
- Hatch (https://hatch.pypa.io/) for building and running the project. This is a Python project manager that can be installed via `pip install hatch`.
1414

1515
## Deployment Steps
16-
1. From the project root directory, run `hatch run lambda-layer:install`. This will create a virtual Python environment in the `layer` directory and install the project dependencies via pip.
17-
2. Package the AWS layer. In the `layer` directory, run `./package.sh`. This will make two files: `python311_layer_content.zip` and `mpic_coordinator_layer_content.zip` which will later be referenced by Open Tofu.
18-
3. Zip all functions. AWS Lambda functions are usually deployed from zip files. cd to the main project directory and then run `./zip-all.sh`
19-
4. Create `config.yaml` in the root directory of the repo to contain the proper values needed for the deployment. A default config.yaml for a 6-perspective deployment with the controller in us-east-2 is included in this repo as `config.example.yaml`. This config can be made the active config by running `cp config.example.yaml config.yaml` in the root directory.
20-
5. Run `hatch run ./configure.py` from the root directory of the repo to generate Open Tofu files from templates.
16+
1. Create `config.yaml` in the root directory of the repo to contain the proper values needed for the deployment. A default config.yaml for a 6-perspective deployment with the controller in us-east-2 is included in this repo as `config.example.yaml`. This config can be made the active config by running `cp config.example.yaml config.yaml` in the root directory.
17+
2. Create a virtual Python environment in the `layer` directory and install the project dependencies via pip. This can be executed by running `hatch run lambda:layer-install`.
18+
3. Package two AWS layers by executing `package-layer.sh`. This will make two files: `python3_layer_content.zip` and `mpic_coordinator_layer_content.zip` which will later be referenced by Open Tofu. This can be done by running `./package-layer.sh` or `hatch run lambda:layer-package`.
19+
4. Run `configure.py` from the root directory of the repo to generate Open Tofu files from templates. This can be separately executed by running `hatch run ./configure.py` or `hatch run lambda:configure-tf`.
20+
5. Zip all Lambda functions. AWS Lambda functions are usually deployed from zip files. This can be separately executed by running `./zip-all.sh` or `hatch run lambda:zip-all`.
2121
6. Deploy the entire package with Open Tofu. cd to the `open-tofu` directory where .tf files are located. Then run `tofu init`. Then run `tofu apply` and type `yes` at the confirmation prompt. This provides a standard install with DNSSEC enabled which causes the system to incur expenses even when it is not in use (due to the AWS VPC NAT Gateways needed). To reduce the AWS bill, DNSSEC can also be disabled by appending `-var="dnssec_enabled=false"` to `tofu apply` (i.e., `tofu apply -var="dnssec_enabled=false"`).
2222
7. Get the URL of the deployed API endpoint by running `hatch run ./get_api_url.py` in the root directory.
2323
8. Get the API Key generated by AWS by running `hatch run ./get_api_key.py` in the root directory. The deployment is configured to reject any API call that does not have this key passed via the `x-api-key` HTTP header.
2424

25-
For convenience `./deploy.sh` in the project root will perform all of these steps (using `-var="dnssec_enabled=false"`) with the exception of copying over the example config to the operational config and running `tofu init` in the open-tofu dir.
25+
For convenience:
26+
* `./deploy.sh` in the project root will clean the environment and perform steps 2-6 (using `-var="dnssec_enabled=false"`), with the exception of copying over the example config to the operational config and running `tofu init` in the open-tofu dir.
27+
* `hatch run lambda:prepare` will run steps 2-5 in a single command.
28+
* `hatch run lambda:deploy` will clean the environment and then run steps 2-6, in the same manner as `deploy.sh`.
2629

2730
## Testing
2831
The following is an example of a test API call that uses bash command substitution to fill in the proper values for the API URL and the API key.

clean.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ FUNCTIONS_DIR="src/aws_lambda_mpic"
77

88
rm open-tofu/*.generated.tf
99

10-
rm -r layer/create_layer_virtualenv
11-
rm -r layer/python311_layer_content
12-
rm -r layer/mpic_coordinator_layer_content
13-
1410
rm layer/*.zip
11+
rm -r layer/create_layer_virtualenv
1512

1613
rm "${FUNCTIONS_DIR}"/mpic_coordinator_lambda/mpic_coordinator_lambda.zip
1714
rm "${FUNCTIONS_DIR}"/mpic_caa_checker_lambda/mpic_caa_checker_lambda.zip

deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
./clean.sh; hatch run lambda-layer:install; cd layer; ./package.sh; cd ..; hatch run ./configure.py; ./zip-all.sh; cd open-tofu; tofu apply -var="dnssec_enabled=false" -auto-approve; cd ..
2+
./clean.sh; hatch env prune; hatch run lambda-layer:install; ./package-layer.sh; hatch run ./configure.py; ./zip-all.sh; cd open-tofu; tofu apply -var="dnssec_enabled=false" -auto-approve; cd ..

layer/package.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

open-tofu/aws-perspective.tf.template

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Each layer must be created in the region of the functions.
2-
resource "aws_lambda_layer_version" "python311_open_mpic_layer_{{region}}" {
3-
filename = "../layer/python311_layer_content.zip"
4-
layer_name = "python311_open_mpic_layer_{{region}}_{{deployment-id}}"
5-
source_code_hash = "${filebase64sha256("../layer/python311_layer_content.zip")}"
2+
resource "aws_lambda_layer_version" "python3_open_mpic_layer_{{region}}" {
3+
filename = "../layer/python3_layer_content.zip"
4+
layer_name = "python3_open_mpic_layer_{{region}}_{{deployment-id}}"
5+
source_code_hash = "${filebase64sha256("../layer/python3_layer_content.zip")}"
66
compatible_runtimes = ["python3.11"]
77
provider = aws.{{region}}
88
}
@@ -211,7 +211,7 @@ resource "aws_lambda_function" "mpic_dcv_checker_lambda_{{region}}" {
211211
runtime = "python3.11"
212212
architectures = ["arm64"]
213213
layers = [
214-
aws_lambda_layer_version.python311_open_mpic_layer_{{region}}.arn,
214+
aws_lambda_layer_version.python3_open_mpic_layer_{{region}}.arn,
215215
]
216216
vpc_config {
217217
subnet_ids = [for s in aws_subnet.subnet_private_{{region}} : s.id]
@@ -241,7 +241,7 @@ resource "aws_lambda_function" "mpic_caa_checker_lambda_{{region}}" {
241241
runtime = "python3.11"
242242
architectures = ["arm64"]
243243
layers = [
244-
aws_lambda_layer_version.python311_open_mpic_layer_{{region}}.arn,
244+
aws_lambda_layer_version.python3_open_mpic_layer_{{region}}.arn,
245245
]
246246
vpc_config {
247247
subnet_ids = [for s in aws_subnet.subnet_private_{{region}} : s.id]

open-tofu/main.tf.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ provider "aws" {
55
}
66

77
# Python open-mpic layer (contains third party libraries)
8-
resource "aws_lambda_layer_version" "python311_open_mpic_layer" {
9-
filename = "../layer/python311_layer_content.zip"
10-
layer_name = "python_311_open_mpic_layer_{{deployment-id}}"
11-
source_code_hash = "${filebase64sha256("../layer/python311_layer_content.zip")}"
8+
resource "aws_lambda_layer_version" "python3_open_mpic_layer" {
9+
filename = "../layer/python3_layer_content.zip"
10+
layer_name = "python3_open_mpic_layer_{{deployment-id}}"
11+
source_code_hash = "${filebase64sha256("../layer/python3_layer_content.zip")}"
1212
compatible_runtimes = ["python3.11"]
1313
}
1414

@@ -75,7 +75,7 @@ resource "aws_lambda_function" "mpic_coordinator_lambda" {
7575
architectures = ["arm64"]
7676
timeout = 60
7777
layers = [
78-
aws_lambda_layer_version.python311_open_mpic_layer.arn,
78+
aws_lambda_layer_version.python3_open_mpic_layer.arn,
7979
aws_lambda_layer_version.mpic_coordinator_layer.arn,
8080
]
8181
environment {

open-tofu/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
variable "dnssec_enabled" {
22
type = bool
33
description = "Enable DNSSEC"
4-
default = true
4+
default = false
55
}

package-layer.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
# make common python3 layer for all lambda functions
3+
mkdir -p layer/python3_layer_content/python
4+
cp -r layer/create_layer_virtualenv/lib layer/python3_layer_content/python/
5+
(cd layer/python3_layer_content && zip -r ../python3_layer_content.zip python)
6+
7+
py_exclude=("*.pyc" "*__pycache__*" "*.pyo" "*.pyd")
8+
9+
# make mpic_coordinator lambda layer for mpic coordinator lambda function
10+
mkdir -p layer/mpic_coordinator_layer_content/python
11+
cp -r resources layer/mpic_coordinator_layer_content/python/resources # TODO consider a more elegant approach
12+
# Zip the mpic_coordinator lambda layer
13+
(cd layer/mpic_coordinator_layer_content && zip -r ../mpic_coordinator_layer_content.zip python -x "${py_exclude[@]}")
14+
15+
# clean up, mostly for the IDE which could otherwise detect duplicate code
16+
rm -r layer/python3_layer_content
17+
rm -r layer/mpic_coordinator_layer_content

pyproject.toml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ classifiers = [
2626
"Programming Language :: Python :: Implementation :: PyPy",
2727
]
2828
dependencies = [
29-
#"open-mpic-core @ git+https://github.com/open-mpic/open-mpic-core-python.git@ds-trace-logging",
29+
# "open-mpic-core @ git+https://github.com/open-mpic/open-mpic-core-python.git@birgelee-dcv-caa-perspective-code-response",
3030
"pyyaml==6.0.1",
3131
"requests>=2.32.3",
3232
"dnspython==2.6.1",
3333
"pydantic==2.8.2",
3434
"aiohttp==3.11.11",
3535
"aws-lambda-powertools[parser]==3.2.0",
36-
"open-mpic-core==4.6.1",
36+
"open-mpic-core==5.0.0",
3737
"aioboto3~=13.3.0",
38+
"black==24.8.0",
3839
]
3940

4041
[project.optional-dependencies]
@@ -54,6 +55,10 @@ Source = "https://github.com/open-mpic/aws-lambda-python"
5455
#[dirs.env]
5556
#virtual = ".hatch"
5657

58+
[tool.api]
59+
spec_version = "3.0.0"
60+
spec_repository = "https://github.com/open-mpic/open-mpic-specification"
61+
5762
[tool.hatch]
5863
version.path = "src/aws_lambda_mpic/__about__.py"
5964
build.sources = ["src", "resources"]
@@ -69,20 +74,37 @@ PIP_INDEX_URL = "https://pypi.org/simple/"
6974
#PIP_EXTRA_INDEX_URL = "https://test.pypi.org/simple/" # FIXME here temporarily to test open-mpic-core packaging
7075
PIP_VERBOSE = "1"
7176

72-
[tool.hatch.envs.lambda-layer]
77+
[tool.hatch.envs.lambda]
7378
skip-install = true
7479
python = "3.11"
7580
type="virtual"
7681
path="layer/create_layer_virtualenv"
7782

78-
[tool.hatch.envs.lambda-layer.env-vars]
83+
[tool.hatch.envs.lambda.env-vars]
7984
#PIP_EXTRA_INDEX_URL = "https://test.pypi.org/simple/"
8085
PIP_ONLY_BINARY = ":all:"
8186
#PIP_PLATFORM = "manylinux2014_aarch64"
8287
#PIP_TARGET = "layer/create_layer_virtualenv2/lib/python3.11/site-packages" # does not work... bug in pip 24.2?
8388

84-
[tool.hatch.envs.lambda-layer.scripts]
85-
install = "pip install . --platform manylinux2014_aarch64 --only-binary=:all: --target layer/create_layer_virtualenv/lib/python3.11/site-packages"
89+
[tool.hatch.envs.lambda.scripts]
90+
layer-install = "pip install . --platform manylinux2014_aarch64 --only-binary=:all: --target layer/create_layer_virtualenv/lib/python3.11/site-packages"
91+
layer-package = "sh ./package-layer.sh"
92+
configure-tf = "python configure.py"
93+
zip-lambdas = "sh ./zip-all.sh"
94+
apply-tf = "(cd open-tofu && tofu apply -var=\"dnssec_enabled=false\" -auto-approve)"
95+
prepare = [
96+
"layer-install",
97+
"layer-package",
98+
"configure-tf",
99+
"zip-lambdas"
100+
]
101+
clean = "sh ./clean.sh"
102+
deploy = [
103+
"clean",
104+
"prepare",
105+
"apply-tf"
106+
]
107+
86108

87109
[tool.hatch.envs.test]
88110
skip-install = false
@@ -135,3 +157,6 @@ include_namespace_packages = true
135157
omit = [
136158
"*/src/*/__about__.py",
137159
]
160+
161+
[tool.black]
162+
line-length = 120

src/aws_lambda_mpic/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.4.0"
1+
__version__ = "0.4.1"

0 commit comments

Comments
 (0)