Monday, September 2, 2024

Dockerfile and Steps to build Docker image for your Spring Boot project

Dockerfile and Steps to build Docker image for your Spring Boot project


To build a Docker image for your Spring Boot project, follow these steps:


 Prerequisites

1. Docker installed on your machine.

2. A built Spring Boot JAR file in your `target` directory (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).

3. A Dockerfile in the root directory of your project (see the Dockerfile example below).


 Step-by-Step Instructions


1. Navigate to the Root Directory of Your Project

   Open a terminal and go to the root directory where your `Dockerfile` is located:


   ```bash

   cd /path/to/your/project

   ```


2. Build the Spring Boot JAR

   Make sure that the Spring Boot JAR file is available in the `target` directory. If not, build it using Maven:


   ```bash

   mvn clean package

   ```


   After running this command, a JAR file will be created in the `target` folder (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).


3. Build the Docker Image

   Use the `docker build` command to build the Docker image:


   ```bash

   docker build -t springboot-app .

   ```


   - `-t springboot-app`: The `-t` flag is used to name the image. Here, `springboot-app` is the name of your Docker image.

   - `.`: The period (`.`) at the end specifies the current directory as the build context, where the Dockerfile is located.


4. Verify the Docker Image

   After the build is complete, verify that the image was created using the `docker images` command:


   ```bash

   docker images

   ```


   You should see an entry similar to the following:


   ```

   REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

   springboot-app      latest              123abc456def        5 minutes ago       500MB

   ```


5. Run the Docker Container

   Once the Docker image is built, you can run a container using the `docker run` command:


   ```bash

   docker run -p 8080:8080 springboot-app

   ```


   - `-p 8080:8080`: Maps port 8080 on your local machine to port 8080 in the Docker container.

   - `springboot-app`: The name of the Docker image you built.


6. Access Your Spring Boot Application

   Open a web browser and navigate to:


   ```

   http://localhost:8080

   ```


   You should see your Spring Boot application running!


 Additional Tips


- Tagging the Image with Versions: You can tag the image with a specific version using `:version`:


  ```bash

  docker build -t springboot-app:v1.0 .

  ```


- Running with Environment Variables: You can pass environment variables to the container using the `-e` flag:


  ```bash

  docker run -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=prod" springboot-app

  ```


- Running the Container in Detached Mode: Use the `-d` flag to run the container in detached mode:


  ```bash

  docker run -d -p 8080:8080 springboot-app

  ```

Here's a `Dockerfile` using `openjdk:17` as the base image and including environment variables configuration.


Dockerfile Contents

```dockerfile

# Use the official OpenJDK 17 image

FROM openjdk:17-jdk-slim


# Set the working directory inside the container

WORKDIR /app


# Copy the Spring Boot JAR file into the container

COPY target/*.jar app.jar


# Expose the port that the Spring Boot application runs on (optional, defaults to 8080)

EXPOSE 8080


# Set environment variables (optional: add your specific environment variables here)

ENV SPRING_PROFILES_ACTIVE=prod \

    JAVA_OPTS="-Xms256m -Xmx512m" \

    APP_NAME="springboot-app"


# Run the Spring Boot application using the environment variables

ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar app.jar"]

```


 Key Components Explained

1. `FROM openjdk:17-jdk-slim`:

   - Uses the official OpenJDK 17 image (`slim` variant) for a lightweight build.

   

2. `WORKDIR /app`:

   - Sets the working directory inside the container to `/app`.


3. `COPY target/*.jar app.jar`:

   - Copies the built Spring Boot JAR file (`*.jar`) from the `target` directory into the `/app` directory inside the container, renaming it to `app.jar`.


4. `EXPOSE 8080`:

   - Opens port `8080` on the container to allow external traffic to reach the application. This is optional but helps document the expected port.


5. `ENV ...`:

   - Adds environment variables to the Docker image.

   - `SPRING_PROFILES_ACTIVE`: Sets the Spring Boot profile (e.g., `dev`, `test`, `prod`).

   - `JAVA_OPTS`: Allows you to pass JVM options, such as memory settings or GC options.

   - `APP_NAME`: A custom environment variable to hold the name of the application.


6. `ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar app.jar"]`:

   - Runs the JAR file using `java -jar` and includes the specified JVM options (`JAVA_OPTS`).

   - `sh -c` allows the `JAVA_OPTS` variable to be evaluated at runtime.


 


To build a Docker image for your Spring Boot project, follow these steps:


 Prerequisites

1. Docker installed on your machine.

2. A built Spring Boot JAR file in your `target` directory (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).

3. A Dockerfile in the root directory of your project (see the previous Dockerfile example).


 Step-by-Step Instructions


1. Navigate to the Root Directory of Your Project

   Open a terminal and go to the root directory where your `Dockerfile` is located:


   ```bash

   cd /path/to/your/project

   ```


2. Build the Spring Boot JAR

   Make sure that the Spring Boot JAR file is available in the `target` directory. If not, build it using Maven:


   ```bash

   mvn clean package

   ```


   After running this command, a JAR file will be created in the `target` folder (e.g., `target/demo-0.0.1-SNAPSHOT.jar`).


3. Build the Docker Image

   Use the `docker build` command to build the Docker image:


   ```bash

   docker build -t springboot-app .

   ```


   - `-t springboot-app`: The `-t` flag is used to name the image. Here, `springboot-app` is the name of your Docker image.

   - `.`: The period (`.`) at the end specifies the current directory as the build context, where the Dockerfile is located.


4. Verify the Docker Image

   After the build is complete, verify that the image was created using the `docker images` command:


   ```bash

   docker images

   ```


   You should see an entry similar to the following:


   ```

   REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

   springboot-app      latest              123abc456def        5 minutes ago       500MB

   ```


5. Run the Docker Container

   Once the Docker image is built, you can run a container using the `docker run` command:


   ```bash

   docker run -p 8080:8080 springboot-app

   ```


   - `-p 8080:8080`: Maps port 8080 on your local machine to port 8080 in the Docker container.

   - `springboot-app`: The name of the Docker image you built.


6. Access Your Spring Boot Application

   Open a web browser and navigate to:


   ```

   http://localhost:8080

   ```


   You should see your Spring Boot application running!


 Additional Tips


- Tagging the Image with Versions: You can tag the image with a specific version using `:version`:


  ```bash

  docker build -t springboot-app:v1.0 .

  ```


- Running with Environment Variables: You can pass environment variables to the container using the `-e` flag:


  ```bash

  docker run -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=prod" springboot-app

  ```


- Running the Container in Detached Mode: Use the `-d` flag to run the container in detached mode:


  ```bash

  docker run -d -p 8080:8080 springboot-app

  ```

Wednesday, August 21, 2024

AWS Glue and Machine Learning to Encrypt PII Data

 

Key Points:

  1. Download S3 File: The download_s3_file function reads the file from S3 into a pandas DataFrame.
  2. Encryption: The encrypt_data function encrypts SSN and credit card information using the KMS key.
  3. Processing: The process_and_encrypt_pii function applies encryption and removes sensitive fields.
  4. Save as Parquet: The save_as_parquet function converts the DataFrame to a Parquet file.
  5. Upload to S3: The upload_parquet_to_s3 function uploads the Parquet file back to S3.
  6. ML Model Loading and Prediction:
    1. The apply_ml_model function loads a pre-trained ML model using joblib and applies it to the DataFrame. The model's prediction is added as a new column to the DataFrame
  7. ML Model Path:
    • The ml_model_path variable specifies the location of your pre-trained ML model (e.g., a .pkl file).

Prerequisites:

  • You need to have a pre-trained ML model saved as a .pkl file. The model should be trained and serialized using a library like scikit-learn.
  • Make sure the feature set used by the ML model is compatible with the DataFrame after encryption.

import boto3
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from botocore.exceptions import ClientError
from cryptography.fernet import Fernet
import base64
import io
from sklearn.externals import joblib  # for loading the ML model

# Initialize the AWS services
s3 = boto3.client('s3')
kms = boto3.client('kms')

def download_s3_file(bucket_name, file_key):
    """Download file from S3 and return its contents as a pandas DataFrame."""
    try:
        obj = s3.get_object(Bucket=bucket_name, Key=file_key)
        df = pd.read_csv(io.BytesIO(obj['Body'].read()))  # Assuming the file is in CSV format
        return df
    except ClientError as e:
        print(f"Error downloading file from S3: {e}")
        raise

def encrypt_data(kms_key_id, data):
    """Encrypt data using AWS KMS."""
    response = kms.encrypt(KeyId=kms_key_id, Plaintext=data.encode())
    encrypted_data = base64.b64encode(response['CiphertextBlob']).decode('utf-8')
    return encrypted_data

def process_and_encrypt_pii(df, kms_key_id):
    """Encrypt SSN and credit card information in the DataFrame."""
    df['encrypted_ssn'] = df['ssn'].apply(lambda x: encrypt_data(kms_key_id, x))
    df['encrypted_credit_card'] = df['credit_card'].apply(lambda x: encrypt_data(kms_key_id, x))

    # Drop original sensitive columns
    df = df.drop(columns=['ssn', 'credit_card'])
    return df

def apply_ml_model(df, model_path):
    """Apply a pre-trained ML model to the DataFrame."""
    # Load the ML model (assuming it's a scikit-learn model saved with joblib)
    model = joblib.load(model_path)
    
    # Assuming the model predicts a column called 'prediction'
    features = df.drop(columns=['encrypted_ssn', 'encrypted_credit_card'])  # Adjust based on your feature set
    df['prediction'] = model.predict(features)
    
    return df

def save_as_parquet(df, output_file_path):
    """Save the DataFrame as a Parquet file."""
    table = pa.Table.from_pandas(df)
    pq.write_table(table, output_file_path)

def upload_parquet_to_s3(bucket_name, output_file_key, file_path):
    """Upload the Parquet file to an S3 bucket."""
    try:
        s3.upload_file(file_path, bucket_name, output_file_key)
        print(f"Successfully uploaded Parquet file to s3://{bucket_name}/{output_file_key}")
    except ClientError as e:
        print(f"Error uploading Parquet file to S3: {e}")
        raise

def main():
    # S3 bucket and file details
    input_bucket = 'your-input-bucket-name'
    input_file_key = 'path/to/your/input-file.csv'
    output_bucket = 'your-output-bucket-name'
    output_file_key = 'path/to/your/output-file.parquet'
    
    # KMS key ID
    kms_key_id = 'your-kms-key-id'

    # ML model path
    ml_model_path = 'path/to/your/ml-model.pkl'
    
    # Local output file path
    local_output_file = '/tmp/output-file.parquet'

    # Download the file from S3
    df = download_s3_file(input_bucket, input_file_key)

    # Encrypt sensitive information
    encrypted_df = process_and_encrypt_pii(df, kms_key_id)

    # Apply the ML model
    final_df = apply_ml_model(encrypted_df, ml_model_path)

    # Save the DataFrame as a Parquet file
    save_as_parquet(final_df, local_output_file)

    # Upload the Parquet file back to S3
    upload_parquet_to_s3(output_bucket, output_file_key, local_output_file)

if __name__ == "__main__":
    main()



Thursday, May 2, 2024

Sentiment Analysis using NLP - Java SDK for Amazon Bedrock/Amazon Sagemaker

Sentiment analysis is a natural language processing (NLP) technique used to determine the sentiment or emotional tone expressed in a piece of text. It involves analyzing text data to classify it into categories such as positive, negative, or neutral sentiments.


Here's a basic overview of how sentiment analysis using NLP works:


1. Text Preprocessing: The text data is preprocessed to remove noise, such as special characters, punctuation, and stopwords (commonly occurring words like "the", "is", "and", etc.). Additionally, text may be converted to lowercase for consistency.


2. Feature Extraction: Features are extracted from the preprocessed text data. These features could be individual words (unigrams), combinations of words (bigrams, trigrams), or other linguistic features.


3. Sentiment Classification: Machine learning models, such as classification algorithms like Support Vector Machines (SVM), Naive Bayes, or deep learning models like Recurrent Neural Networks (RNNs) or Transformers, are trained using labeled data. Labeled data consists of text samples along with their corresponding sentiment labels (positive, negative, or neutral).


4. Model Training: The extracted features are used to train the sentiment analysis model. During training, the model learns to recognize patterns in the text data that are indicative of specific sentiments.


5. Model Evaluation: The trained model is evaluated using a separate set of labeled data (validation or test set) to assess its performance in classifying sentiments accurately. Evaluation metrics such as accuracy, precision, recall, and F1-score are commonly used to measure the model's effectiveness.


6. Inference: Once the model is trained and evaluated, it can be used to perform sentiment analysis on new, unseen text data. The model predicts the sentiment of each text sample, classifying it as positive, negative, or neutral.


Sentiment analysis has various applications across different domains, including:


- Customer feedback analysis: Analyzing customer reviews, comments, or social media posts to understand customer sentiment towards products or services.

- Brand monitoring: Monitoring online mentions and discussions to gauge public sentiment towards a brand or organization.

- Market research: Analyzing sentiment in news articles, blogs, or social media discussions to assess market trends and consumer preferences.

- Voice of the customer (VoC) analysis: Extracting insights from customer surveys or feedback forms to identify areas for improvement and measure customer satisfaction.


Overall, sentiment analysis using NLP enables businesses and organizations to gain valuable insights from text data, helping them make data-driven decisions and enhance customer experiences.


To utilize AWS Bedrock for NLP (Natural Language Processing) in Java, you can use the AWS SDK for Java. Below is a basic example code snippet demonstrating how to use AWS Bedrock APIs for NLP tasks like sentiment analysis:


import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.sagemaker.AmazonSageMaker; import com.amazonaws.services.sagemaker.AmazonSageMakerClientBuilder; import com.amazonaws.services.sagemaker.model.InvokeEndpointRequest; import com.amazonaws.services.sagemaker.model.InvokeEndpointResult; public class BedrockNLPExample { public static void main(String[] args) { // Replace these values with your AWS credentials and SageMaker endpoint String accessKey = "YOUR_ACCESS_KEY"; String secretKey = "YOUR_SECRET_KEY"; String endpointUrl = "YOUR_SAGEMAKER_ENDPOINT_URL"; // Initialize AWS credentials BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); // Create an instance of SageMaker client AmazonSageMaker sageMakerClient = AmazonSageMakerClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpointUrl, "us-west-2")) // Change region if needed .build(); // Sample text for sentiment analysis String text = "I love using AWS services."; // Invoke endpoint for sentiment analysis InvokeEndpointRequest request = new InvokeEndpointRequest() .withEndpointName("YOUR_SAGEMAKER_ENDPOINT_NAME") // Replace with your SageMaker endpoint name .withContentType("text/csv") .withBody(text); InvokeEndpointResult result = sageMakerClient.invokeEndpoint(request); // Process the result String responseBody = new String(result.getBody().array()); System.out.println("Sentiment Analysis Result: " + responseBody); } }

This code assumes you have already set up an endpoint in AWS SageMaker for NLP tasks, such as sentiment analysis. It sends a request to the SageMaker endpoint with the text to analyze and prints the result. Ensure that you have necessary permissions and that your SageMaker endpoint is properly configured to handle the request.



Thursday, March 14, 2024

OCI Knowledge Series: OCI Infrastructure components

 Oracle Cloud Infrastructure (OCI) provides a comprehensive set of infrastructure services that enable you to build and run a wide range of applications in a highly available, secure, and scalable environment. Below are the various components of OCI infrastructure:


These are some of the key components of the Oracle Cloud Infrastructure (OCI) that enable you to build and manage your cloud infrastructure and application

1. Regions: A region is a localized geographic area composed of one or more availability domains. Regions are isolated from each other, and they are independent of each other in terms of fault tolerance and availability. Each region contains multiple data centers called availability domains. 2. Availability Domains (AD): An availability domain is a standalone, independent data center within a region. Availability domains are isolated from each other, with their own power, cooling, and networking infrastructure. This isolation enhances fault tolerance and availability. OCI services deployed within a region are designed to be resilient to failures within an availability domain. 3. Virtual Cloud Network (VCN): A VCN is a customizable, private network within OCI where you can launch your compute instances, block storage, and other resources. It is logically isolated from other virtual networks in the OCI environment, providing you with control over your network settings, such as IP addressing, route tables, and gateways. 4. Subnets: Subnets are subdivisions of a VCN and represent segmented portions of your network. You can divide a VCN into one or more subnets to host different types of resources. Subnets can be public or private, depending on whether they have internet connectivity. 5. Compute Instances: Compute instances, also known as virtual machines (VMs), are virtualized computing environments where you can run your applications. OCI offers various types of compute instances, including generalpurpose, highperformance, and GPU instances, suited for different workload requirements. 6. Block Storage: OCI provides block storage services for storing persistent data. Block volumes can be attached to compute instances as additional disks to provide scalable and highperformance storage. 7. Object Storage: OCI Object Storage is a highly scalable and durable storage service for storing unstructured data, such as documents, images, and videos. It provides a costeffective solution for storing and retrieving large amounts of data. 8. Networking Services: OCI offers a variety of networking services, including load balancers, DNS, VPN, and FastConnect, to enable secure and efficient communication between resources within your VCN and with external networks. 9. Database Services: OCI provides fully managed database services, including Oracle Autonomous Database, MySQL, and NoSQL Database, to support different types of database workloads. 10. Identity and Access Management (IAM): IAM is a centralized service for managing user access and permissions in OCI. It enables you to define and enforce security policies, roles, and permissions to control who can access which resources and perform specific actions. 11. Security Services: OCI offers a range of security services, such as Web Application Firewall (WAF), Key Management, and Security Zones, to protect your applications and data from security threats. 12. Monitoring and Management Tools: OCI provides monitoring, logging, and management tools, including OCI Monitoring, Logging, and Resource Manager, to help you monitor, troubleshoot, and manage your resources effectively. These are some of the key components of the Oracle Cloud Infrastructure (OCI) that enable you to build and manage your cloud infrastructure and application

OCI (Oracle Cloud Infrastructure) SDK to provision VCN, Subnet and establish VPN connectivity

 Oracle Cloud Infrastructure (OCI) Virtual Cloud Network (VCN) is the networking layer of the Oracle Cloud Infrastructure, equivalent to the Virtual Private Cloud (VPC) in other cloud providers. A VCN allows you to set up a customizable and private network in Oracle’s cloud. You can control the VCN’s IP address range, create subnets, and configure route tables and gateways to manage traffic within or outside the VCN.

  1. Private and Isolated Network: A VCN provides an isolated network within the Oracle Cloud Infrastructure.
  2. Customizable: You can set the IP CIDR block, create subnets, and use Network Security Groups or Security Lists to control inbound and outbound traffic.
  3. Route Tables: Define how the traffic is routed within your VCN or to the internet.
  4. Internet Gateway: Allows traffic to flow between your VCN and the internet.
  5. NAT Gateway: Allows instances in a private subnet to initiate outbound connections to the internet without exposing their IP addresses.
  6. VPN Gateway: For secure, encrypted communication between your on-premise network and your VCN.
  7. Load Balancer: Distributes incoming traffic across multiple targets to ensure high availability.
  8. Service Gateway: Provides a path for private traffic between your VCN and supported Oracle services.


This code snippet creates a VCN, subnet, security list, and VPN using the OCI Java SDK, utilizing the Identity service client and the respective create methods. Make sure to handle exceptions appropriately in your production code.


Make sure to replace "your_compartment_id", "YourVCN", "YourSubnet", "YourSecurityList", and "YourVPN" with appropriate values for your Oracle Cloud tenancy, Virtual Cloud Network (VCN), subnet, security list, and VPN display names respectively.

Ensure that your OCI configuration file (typically found at ~/.oci/config) is properly configured with your user credentials and the correct region.


import com.oracle.bmc.identity.IdentityClient;

import com.oracle.bmc.identity.model.CreateVpnDetails;

import com.oracle.bmc.identity.model.CreateSubnetDetails;

import com.oracle.bmc.identity.model.CreateSecurityListDetails;

import com.oracle.bmc.identity.model.CreateSecurityRuleDetails;

import com.oracle.bmc.identity.requests.CreateVpnRequest;

import com.oracle.bmc.identity.requests.CreateSubnetRequest;

import com.oracle.bmc.identity.requests.CreateSecurityListRequest;

import com.oracle.bmc.identity.responses.CreateVpnResponse;

import com.oracle.bmc.identity.responses.CreateSubnetResponse;

import com.oracle.bmc.identity.responses.CreateSecurityListResponse;

import com.oracle.bmc.Region;

import com.oracle.bmc.auth.AuthenticationDetailsProvider;

import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;

import com.oracle.bmc.model.BmcException;


import java.util.Collections;


public class InfrastructureProvisioning {

    public static void main(String[] args) {

        String compartmentId = "your_compartment_id";

        String vcnDisplayName = "YourVCN";

        String subnetDisplayName = "YourSubnet";

        String securityListDisplayName = "YourSecurityList";

        String vpnDisplayName = "YourVPN";


        // Path to your OCI configuration file

        String configurationFilePath = "~/.oci/config";


        // Get the authentication details from the OCI configuration file

        AuthenticationDetailsProvider provider =

                new ConfigFileAuthenticationDetailsProvider(configurationFilePath, "DEFAULT");


        IdentityClient identityClient = new IdentityClient(provider);

        identityClient.setRegion(Region.US_PHOENIX_1); // Change to appropriate region


        try {

            // Create VCN

            CreateVcnDetails createVcnDetails = CreateVcnDetails.builder()

                    .cidrBlock("10.0.0.0/16")

                    .compartmentId(compartmentId)

                    .displayName(vcnDisplayName)

                    .build();


            CreateVcnRequest createVcnRequest = CreateVcnRequest.builder()

                    .createVcnDetails(createVcnDetails)

                    .build();


            Vcn vcn = identityClient.createVcn(createVcnRequest).getVcn();


            // Create Subnet

            CreateSubnetDetails createSubnetDetails = CreateSubnetDetails.builder()

                    .cidrBlock("10.0.0.0/24")

                    .compartmentId(compartmentId)

                    .displayName(subnetDisplayName)

                    .vcnId(vcn.getId())

                    .build();


            CreateSubnetRequest createSubnetRequest = CreateSubnetRequest.builder()

                    .createSubnetDetails(createSubnetDetails)

                    .build();


            Subnet subnet = identityClient.createSubnet(createSubnetRequest).getSubnet();


            // Create Security List

            CreateSecurityRuleDetails createSecurityRuleDetails = CreateSecurityRuleDetails.builder()

                    .direction(CreateSecurityRuleDetails.Direction.Egress)

                    .destination("0.0.0.0/0")

                    .protocol("all")

                    .build();


            CreateSecurityListDetails createSecurityListDetails = CreateSecurityListDetails.builder()

                    .compartmentId(compartmentId)

                    .displayName(securityListDisplayName)

                    .egressSecurityRules(Collections.singletonList(createSecurityRuleDetails))

                    .ingressSecurityRules(Collections.singletonList(createSecurityRuleDetails))

                    .vcnId(vcn.getId())

                    .build();


            CreateSecurityListRequest createSecurityListRequest = CreateSecurityListRequest.builder()

                    .createSecurityListDetails(createSecurityListDetails)

                    .build();


            SecurityList securityList = identityClient.createSecurityList(createSecurityListRequest).getSecurityList();


            // Create VPN

            CreateVpnDetails createVpnDetails = CreateVpnDetails.builder()

                    .compartmentId(compartmentId)

                    .displayName(vpnDisplayName)

                    .vcnId(vcn.getId())

                    .build();


            CreateVpnRequest createVpnRequest = CreateVpnRequest.builder()

                    .createVpnDetails(createVpnDetails)

                    .build();


            Vpn vpn = identityClient.createVpn(createVpnRequest).getVpn();


            System.out.println("VPN Created: " + vpn.getId());

            System.out.println("Subnet Created: " + subnet.getId());

            System.out.println("Security List Created: " + securityList.getId());

        } catch (BmcException e) {

            System.out.println("Error: " + e.getMessage());

        } finally {

            identityClient.close();

        }

    }

}


Wednesday, December 13, 2023

TypeScript-first schema declaration using ZOD

Zod is a TypeScript-first schema declaration and validation library used to define the shape of data in TypeScript. It allows you to create schemas for your data structures, validate incoming data against those schemas, and ensure type safety within your TypeScript applications.


Here's a simple example demonstrating how Zod can be used:


Typescript code

import * as z from 'zod';


// Define a schema for a user object

const userSchema = z.object({

  id: z.string(),

  username: z.string(),

  email: z.string().email(),

  age: z.number().int().positive(),

  isAdmin: z.boolean(),

});


// Data to be validated against the schema

const userData = {

  id: '123',

  username: 'johndoe',

  email: 'john@example.com',

  age: 30,

  isAdmin: true,

};


// Validate the data against the schema

try {

  const validatedUser = userSchema.parse(userData);

  console.log('Validated user:', validatedUser);

} catch (error) {

  console.error('Validation error:', error);

}

```


In the above example:


1. We import `z` from 'zod', which provides access to Zod's functionality.

2. We define a schema for a user object using `z.object()`. Each property in the object has a specific type and validation constraint defined by Zod methods like `z.string()`, `z.number()`, `z.boolean()`, etc.

3. `userData` represents an object we want to validate against the schema.

4. We use `userSchema.parse()` to validate `userData` against the defined schema. If the data matches the schema, it returns the validated user object; otherwise, it throws a validation error.


Zod helps ensure that the incoming data adheres to the defined schema, providing type safety and validation within TypeScript applications. This prevents runtime errors caused by unexpected data shapes or types.

Monday, December 11, 2023

AWS Glue Job to read data from Amazon Kinesis

 Here's an example of how to use AWS Glue to read from an Amazon Kinesis stream using PySpark. AWS Glue can be used to create ETL (Extract, Transform, Load) jobs to process data from Kinesis streams.

First, make sure you have the necessary AWS Glue libraries and dependencies. You will also need permission from the AWS Glue service to access your Kinesis stream.

Here is a basic example of how to set up a Glue job to read from a Kinesis stream:


import sys from awsglue.transforms import * from awsglue.utils import getResolvedOptions from pyspark.context import SparkContext from awsglue.context import GlueContext from awsglue.job import Job from awsglue.dynamicframe import DynamicFrame import json # Initialize the Glue context and Spark session args = getResolvedOptions(sys.argv, ['JOB_NAME']) sc = SparkContext() glueContext = GlueContext(sc) spark = glueContext.spark_session job = Job(glueContext) job.init(args['JOB_NAME'], args) # Define the Kinesis stream parameters stream_name = "your_kinesis_stream_name" region_name = "your_region_name" # Create a DynamicFrame from the Kinesis stream data_frame = glueContext.create_data_frame.from_catalog( database="your_database", table_name="your_table" ) # Convert DynamicFrame to DataFrame df = data_frame.toDF() # Perform transformations on the DataFrame # For example, if your Kinesis data is in JSON format, you might need to parse it parsed_df = df.rdd.map(lambda x: json.loads(x["data"])).toDF() # Show the parsed data parsed_df.show() # Write the data to an S3 bucket or another destination output_path = "s3://your_output_bucket/output_path/" parsed_df.write.format("json").save(output_path) # Commit the job job.commit()

Explanation:

  1. Initialize Glue context and Spark session: This sets up the necessary context for running Glue jobs.
  2. Define Kinesis stream parameters: Specify your Kinesis stream name and region.
  3. Create a DynamicFrame: Use Glue's create_data_frame method to read from the Kinesis stream.
  4. Transformations: Parse the JSON data or perform other transformations as required.
  5. Write the data: Save the transformed data to an S3 bucket or another desired destination.
  6. Commit the job: This finalizes the Glue job.

Prerequisites:

  • Ensure you have the AWS Glue, AWS Kinesis, and PySpark libraries installed.
  • You need appropriate permissions for AWS Glue to access the Kinesis stream and S3 buckets.
  • Replace placeholders like your_kinesis_stream_name, your_region_name, your_database, your_table, and s3://your_output_bucket/output_path/ with actual values specific to your setup.

Make sure to test this script in your AWS Glue environment, as the configuration might vary based on your specific use case and AWS environment settings.

How IdP Groups Are Tied to Databricks Groups (Unity Catalog)

  🔗 How IdP Groups Are Tied to Databricks Groups (Unity Catalog) 🔑 Key Principle (Read This First) Databricks does NOT “map” IdP groups...