How to fetch Currency Exchange data into AWS environments using API

Inserting data into various AWS environments using API's

@Sampath Maddula

10/6/20234 min read

Inserting Currency Exchange Data into Cloud Environments like AWS using APIs

Step 1: Choose a Currency Exchange Rate API

  • A currency exchange API can let you obtain vital information quickly and affordably while also saving you a lot of time and improving operational efficiency. The exchange of currencies is streamlined by this effective instrument. Consequently, convenience and accuracy are provided to both consumers and businesses.

  • Popular currency exchange rate APIs Open Exchange Rates, ExchangeRatesAPI.io .

  • In order to get the data we need to sign up for an API key or subscription.

Step 2: Set Up an AWS S3 Bucket

  • To upload your data to Amazon S3, you must first create an Amazon S3 bucket in one of the AWS Regions. When you create a bucket, you must choose a bucket name and Region. You can optionally choose other storage management options for the bucket. After you create a bucket, you cannot change the bucket name or Region.

  • By default, all Amazon S3 buckets and objects are private. Only the resource owner which is the AWS account that created the bucket can access that bucket. The resource owner can, however, choose to grant access permissions to other resources and users. One way to do this is to write an access policy.

Step 3: Write a Script or Program

  • Before you begin, make sure you have the necessary tools installed:

    • Python: Install Python on your local machine if you haven't already.

    • Boto3: Install the Boto3 library for AWS using pip install boto3.

    • Requests: Install the Requests library for making API requests using pip install requests.

  • Example of a code using a programming language like Python and the Boto3 library for AWS is represented in the image and the link.

  • Replace "YOUR_API_KEY" with the API key you obtained from the currency exchange rate API provider.

  • Replace "your-s3-bucket-name" with the name of your AWS S3 bucket.

  • Modify the s3_object_key to specify the desired object key within the bucket. In this example, we're using the current timestamp as part of the object key to create a unique file name.

  • Execute the script in your terminal: python fetch_currency_data.py .This will fetch the currency exchange data from the API and upload it to your AWS S3 bucket.

Step 4: AWS Lambda

  • Log in to the AWS Management Console: Log in to your AWS account.

  • Navigate to Lambda: Go to the Lambda service by searching for "Lambda" in the AWS Management Console.

  • Create a New Lambda Function:

    • Click the "Create function" button.

    • Choose "Author from scratch."

    • Enter a name for your Lambda function, such as "CurrencyExchangeUpdater."

    • Choose a runtime (e.g., Python, Node.js) depending on the language you're comfortable with.

  • Configure Execution Role:

    • Under "Execution role," choose "Use an existing role."

    • Select an existing role that has the necessary permissions to write to your S3 bucket.

  • Add Code to Your Lambda Function.

    In the Lambda function editor, add the code that fetches currency exchange data from the chosen API and uploads it to your S3 bucket. You can reuse the code from the previous example with some modifications. Be sure to configure environment variables or Lambda function parameters for sensitive information such as your API key and S3 bucket name. This avoids hardcoding sensitive data directly into your code.

  • Set Up a Trigger. To automate the execution of your Lambda function, you need to set up a trigger. In this case, you can use an AWS CloudWatch Events rule to schedule the Lambda function to run at specified intervals. In the Lambda function configuration, click on "Add trigger." Choose "CloudWatch Events" as the trigger type. Configure the rule to run your Lambda function at the desired schedule. For example, you can use a cron expression to run the Lambda function daily at a specific time.

  • Test and Deploy. Test your Lambda function by clicking the "Test" button in the Lambda function editor. Ensure that it successfully fetches currency exchange data and uploads it to your S3 bucket. Deploy your Lambda function by clicking the "Deploy" button. This makes it ready for scheduled execution based on the CloudWatch Events rule you defined earlier.

  • Monitor and Troubleshoot. Monitor your Lambda function's execution by checking the CloudWatch Logs for any errors or issues. If there are errors, review the function's code and configuration to identify and resolve them.

    Automate Currency Data Updates

With your AWS Lambda function and CloudWatch Events rule in place, your currency exchange data will be automatically fetched and updated in your S3 bucket according to the schedule you've defined.

By following these steps, you've created an automated process for keeping your currency exchange data current in your cloud environment. This approach can be adapted to other data sources and APIs, allowing you to maintain up-to-date data for various applications and analyses.