The project implements a currency exchange rate backend component that uses Amazon API Gateway, AWS Lambda and Amazon DynamoDB to fetch the latest exchange rates from European Central Bank Website and provides basic APIs to:
- Get current exchange rates.
- How the exchange rate has changed compared to the previous day.
The project uses AWS CDK for deployment. It was set up using the aws-cdk-project-structure-python
template provided by AWS.
It has the following components:
Dynamodb is used to store exchange rates info. The key is formed by concatenating currency and date. An additional index
of date is defined to allow querying on date as well.
This Lambda function runs every 12 hours. It fetches the latest exchange rates from European Central Bank Website and stores them in dynamodb. It can be changed to run on a cron schedule as per requirement.
This lambda function is tied to API gateway and provides two functionalities.
- Get exchange rates and change in exchange rates for all currencies.
- Get exchange rates and change in exchange rates for a particular currency.
It creates an API gateway and ties to lambda function.
It creates a dashboard in Cloud Watch with two widgets:
- API Gateway metric count
- Dynamodb consumed read capacity unit metric count
Unzip the provided archive file.
unzip python-cdk-apigw-lambda-dynamodb-main
cd python-cdk-apigw-lambda-dynamodb-mainpython3.8 -m venv .venv
source .venv/bin/activate
pip install pip-tools==6.4.0
pip install pip==21.3.1
./scripts/install-deps.shThe CurrencyExchangeRateBackend stack uses AWS account and region setup using CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION environment variables.
export CDK_DEFAULT_ACCOUNT=<YOUR_ACCOUNT>
export CDK_DEFAULT_REGION=<YOUR_REGION>
npx cdk bootstrap
npx cdk deploy CurrencyExchangeRateBackendExample output for npx cdk deploy CurrencyExchangeRateBackend:
✅ CurrencyExchangeRateBackend
Outputs:
CurrencyExchangeRateBackend.APIEndpoint = https://afwaatt0j1.execute-api.us-west-2.amazonaws.com/
CurrencyExchangeRateBackend.UpdateCurrencyRateLambdaFunction = CurrencyExchangeRateBacke-LambdaUpdateCurrencyRate-84C3MQPW6KX6
Stack ARN:
arn:aws:cloudformation:us-west-2:007148109666:stack/CurrencyExchangeRateBackend/f95c7500-745d-11ed-a0c1-0612a202de15
The Update Exchange Rates lambda function executes every 12 hours. However, you may invoke it once, before testing. Below are examples that show the available resources and how to use them.
function_name=$(aws cloudformation describe-stacks \
--stack-name CurrencyExchangeRateBackend \
--query 'Stacks[*].Outputs[?OutputKey==`UpdateCurrencyRateLambdaFunction`].OutputValue' \
--region $CDK_DEFAULT_REGION \
--output text )
aws lambda invoke --function-name ${function_name} out --log-type Tail --region $CDK_DEFAULT_REGION
api_endpoint=$(aws cloudformation describe-stacks \
--stack-name CurrencyExchangeRateBackend \
--query 'Stacks[*].Outputs[?OutputKey==`APIEndpoint`].OutputValue' \
--region $CDK_DEFAULT_REGION \
--output text )
curl "${api_endpoint}/exchange_rates"
curl "${api_endpoint}/exchange_rates/USD"
Do not forget to delete the stacks to avoid unexpected charges
npx cdk destroy CurrencyExchangeRateBackend