Friday, October 27, 2023

AWS DMS and Deployment of tasks using AWS CDK-Python

 AWS DMS stands for Amazon Web Services Database Migration Service. It is a fully managed database migration service that helps you migrate databases to AWS quickly and securely. AWS DMS supports both homogeneous migrations, where the source and target databases are of the same engine (e.g., Oracle to Oracle), and heterogeneous migrations, where the source and target databases are of different engines (e.g., Microsoft SQL Server to Amazon Aurora).

Key features of AWS DMS include:

1. Data Replication: AWS DMS can continuously replicate data changes from the source database to the target database, ensuring that the target remains up to date with changes made in the source.

2. Schema Conversion: For heterogeneous migrations, AWS DMS can help convert schema and data types from the source to the target database to ensure compatibility.

3. Minimized Downtime: It allows you to migrate data with minimal downtime by performing an initial data load and then continually synchronizing changes.

4. Database Cloning: You can use DMS to create a clone of your production database for testing and development purposes.

5. Change Data Capture: AWS DMS can capture changes from popular database engines, such as Oracle, SQL Server, MySQL, PostgreSQL, and more, in real-time.

6. Data Filtering and Transformation: You can configure data filtering and transformation rules to control what data gets migrated and how it's transformed during the migration process.

7. Security and Encryption: AWS DMS provides encryption options to ensure the security of your data during migration.

8. Integration with AWS Services: AWS DMS can be integrated with other AWS services, such as AWS Schema Conversion Tool (SCT), AWS Database Assessment Tool (DAT), and AWS Database Query Tool (DQT), to facilitate the migration process.

Overall, AWS DMS is a versatile tool for simplifying and automating database migrations to AWS, making it easier for organizations to move their databases to the cloud while minimizing disruptions to their applications.


Deployment using AWS CDK

To create an AWS Cloud Development Kit (CDK) stack for AWS Database Migration Service (DMS) in Python, you'll need to define the necessary resources, such as replication instances, endpoints, and migration tasks. Below is a basic example of how to create a DMS stack using AWS CDK. Note that you'll need to have the AWS CDK and AWS CLI configured on your system and also install the necessary CDK modules.

from aws_cdk import core

from aws_cdk import aws_dms as dms

from aws_cdk import aws_secretsmanager as secrets_manager

class DMSStack(core.Stack):

    def init(self, scope: core.Construct, id: str, **kwargs) -> None:

        super().__init__(scope, id, **kwargs)

        # Create a secret to store credentials for the source and target databases

        source_secret = secrets_manager.Secret(

            self, "SourceDatabaseSecret",

            description="Secret for source database connection",

            generate_secret_string=secrets_manager.SecretStringGenerator(

                secret_string_template={"username": "source_username"},

                generate_string_key="password",

                password_length=12,

                exclude_characters='"@/',

            ),

        )

        target_secret = secrets_manager.Secret(

            self, "TargetDatabaseSecret",

            description="Secret for target database connection",

            generate_secret_string=secrets_manager.SecretStringGenerator(

                secret_string_template={"username": "target_username"},

                generate_string_key="password",

                password_length=12,

                exclude_characters='"@/',

            ),

        )

        # Define a replication instance

        replication_instance = dms.CfnReplicationInstance(

            self, "ReplicationInstance",

            replication_instance_class="dms.r5.large",

            allocated_storage=100,

        )

        # Define source and target endpoints

        source_endpoint = dms.CfnEndpoint(

            self, "SourceEndpoint",

            endpoint_identifier="source-endpoint",

            endpoint_type="source",

            engine_name="mysql",

            username=source_secret.secret_value_from_json("username").to_string(),

            password=source_secret.secret_value_from_json("password").to_string(),

            server_name="source-database-server",

            port=3306,

            database_name="source_database",

        )

        target_endpoint = dms.CfnEndpoint(

            self, "TargetEndpoint",

            endpoint_identifier="target-endpoint",

            endpoint_type="target",

            engine_name="aurora",

            username=target_secret.secret_value_from_json("username").to_string(),

            password=target_secret.secret_value_from_json("password").to_string(),

            server_name="target-database-cluster",

            port=3306,

            database_name="target_database",

        )

        # Create a migration task

        migration_task = dms.CfnReplicationTask(

            self, "MigrationTask",

            migration_task_identifier="my-migration-task",

            migration_type="full-load",

            source_endpoint_arn=source_endpoint.attr_endpoint_arn,

            target_endpoint_arn=target_endpoint.attr_endpoint_arn,

            table_mappings="...custom table mapping...",

        )

app = core.App()

DMSStack(app, "DMSStack")

app.synth()


In this code, we create a CDK stack that includes:

1. Secrets for storing database credentials.

2. A replication instance for DMS.

3. Source and target endpoints for the source and target databases.

4. A migration task that specifies the type of migration (full-load) and the endpoints to use.

You'll need to customize this code by providing the actual database connection details and table mappings in the migration task. Additionally, you may need to install the required CDK modules and configure AWS CDK on your system before deploying the stack.

Thursday, October 26, 2023

Install AWS Schema Conversion Tool (SCT) on an Amazon Linux 2

To install the AWS Schema Conversion Tool (SCT) on an Amazon Linux 2 instance, you can follow these steps. The AWS Schema Conversion Tool helps you convert your database schema from one database engine to another, making it easier to migrate your data.


1. Prerequisites:

   - An Amazon Linux 2 instance.

   - AWS account credentials with appropriate permissions to download and install the tool.


2. Connect to Your Amazon Linux 2 Instance:

   You can use SSH to connect to your Amazon Linux 2 instance. Make sure you have the necessary permissions and key pair for accessing the instance.


3. Update Your System:

   It's a good practice to start by updating the package repository and installed packages:

   sudo yum update -y

4. Download and Install AWS SCT:

   You can download and install AWS SCT using `curl` and `yum`:

   sudo curl "https://d1un7b5vff5wnt.cloudfront.net/downloads/AWSSchemaConversionToolSetup-x86_64.bin" -o AWSSchemaConversionToolSetup-x86_64.bin

   sudo chmod +x AWSSchemaConversionToolSetup-x86_64.bin

   sudo ./AWSSchemaConversionToolSetup-x86_64.bin

  

   This will launch the AWS Schema Conversion Tool installer. Follow the installation prompts and choose the installation location. It's recommended to install it in a directory that's in your `PATH` for easier access.


5. Start AWS SCT:

   After the installation is complete, you can start the AWS Schema Conversion Tool:

 

   aws-schema-conversion-tool


6. Configure AWS SCT:

   When you first start AWS SCT, you'll need to configure it by providing your AWS account credentials and configuring connection profiles for your source and target databases.


   Follow the on-screen instructions to set up these configurations.


7. Using AWS SCT:

   You can now use AWS SCT to perform schema conversions and database migrations.


Remember that AWS SCT requires Java, so make sure that Java is installed on your Amazon Linux 2 instance.


Once you've completed these steps, you should have AWS SCT up and running on your Amazon Linux 2 instance, and you can use it to convert and migrate your database schemas.

Wednesday, October 18, 2023

AWS SAM template to deploy lamdas function exposing lambda through API Gateway.

This thread discusses the steps to deploy a Lambda function named "getIdentities" with a layer and expose it through API Gateway using AWS SAM.  The Lambda function fetches data from DynamoDB, you can use the following AWS SAM template. This example assumes you're working with Node.js for your Lambda function and DynamoDB as your database:


YAML 

AWSTemplateFormatVersion: '2010-09-09'

Transform: 'AWS::Serverless-2016-10-31'


Resources:

  MyLambdaLayer:

    Type: AWS::Serverless::LayerVersion

    Properties:

      LayerName: MyLayer

      ContentUri: ./layer/

      CompatibleRuntimes:

        - nodejs14.x

      Description: My Lambda Layer


  MyLambdaFunction:

    Type: AWS::Serverless::Function

    Properties:

      Handler: index.handler

      Runtime: nodejs14.x

      Layers:

        - !Ref MyLambdaLayer

      CodeUri: ./function/

      Description: My Lambda Function

      Environment:

        Variables:

          DYNAMODB_TABLE_NAME: !Ref MyDynamoDBTable

      Events:

        MyApi:

          Type: Api

          Properties:

            Path: /getIdentities

            Method: GET


  MyDynamoDBTable:

    Type: AWS::DynamoDB::Table

    Properties:

      TableName: MyDynamoDBTable

      AttributeDefinitions:

        - AttributeName: id

          AttributeType: S

      KeySchema:

        - AttributeName: id

          KeyType: HASH

      ProvisionedThroughput:

        ReadCapacityUnits: 5

        WriteCapacityUnits: 5


Outputs:

  MyApi:

    Description: "API Gateway endpoint URL"

    Value:

      Fn::Sub: "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/getIdentities"

 


In this SAM template:


1. We define a Lambda Layer resource named `MyLambdaLayer`. You should place your layer code in the `./layer/` directory.


2. We define a Lambda function resource named `MyLambdaFunction`. This function uses the layer created in step 1 and is associated with an API Gateway event at the path "/getIdentities" and HTTP method GET. The function code is located in the `./function/` directory, and the handler is set to `index.handler`. We also set an environment variable to specify the DynamoDB table name.


3. We define a DynamoDB table resource named `MyDynamoDBTable` to store your data. Adjust the table name, schema, and provisioned throughput according to your requirements.


4. The Outputs section provides the URL of the API Gateway endpoint where you can invoke the "getIdentities" Lambda function.


Make sure your project directory structure is organized as follows:



project-directory/

  ├── template.yaml

  ├── function/

  │     ├── index.js

  │     └── package.json

  ├── layer/

  │     ├── layer-files

  └── template-configs/

        ├── parameters.json

        ├── metadata.json



To fetch data from DynamoDB, you can use the AWS SDK for JavaScript in your Lambda function code. Here's a simple example of how you can fetch data from DynamoDB using the Node.js SDK:


const AWS = require('aws-sdk');


const dynamodb = new AWS.DynamoDB.DocumentClient();

const tableName = process.env.DYNAMODB_TABLE_NAME;


exports.handler = async (event) => {

  try {

    const params = {

      TableName: tableName,

      Key: {

        id: 'your-key-here',

      },

    };


    const data = await dynamodb.get(params).promise();


    return {

      statusCode: 200,

      body: JSON.stringify(data.Item),

    };

  } catch (error) {

    return {

      statusCode: 500,

      body: JSON.stringify({ error: error.message }),

    };

  }

};



In this code, we're using the AWS SDK to fetch an item from DynamoDB based on a specified key. You should customize the key and error handling based on your use case.

Amazon Bedrock and AWS Rekognition comparison for Image Recognition

 Both Amazon Bedrock and AWS Rekognition are services provided by AWS, but they cater to different use cases, especially when it comes to ...