#aws boto3 dynamodb
Explore tagged Tumblr posts
akrnd085 · 1 year ago
Text
Boto3 and DynamoDB: Integrating AWS’s NoSQL Service with Python
0 notes
practicallogix · 4 months ago
Text
The Role of the AWS Software Development Kit (SDK) in Modern Application Development
The Amazon Web Services (AWS) Software Development Kit (SDK) serves as a fundamental tool for developers aiming to create robust, scalable, and secure applications using AWS services. By streamlining the complexities of interacting with AWS's extensive ecosystem, the SDK enables developers to prioritize innovation over infrastructure challenges. 
Understanding AWS SDK 
The AWS SDK provides a comprehensive suite of software tools, libraries, and documentation designed to facilitate programmatic interaction with AWS services. By abstracting the intricacies of direct HTTP requests, it offers a more intuitive and efficient interface for tasks such as instance creation, storage management, and database querying. 
The AWS SDK is compatible with multiple programming languages, including Python (Boto3), Java, JavaScript (Node.js and browser), .NET, Ruby, Go, PHP, and C++. This broad compatibility ensures that developers across diverse technical environments can seamlessly integrate AWS features into their applications.
Key Features of AWS SDK
Seamless Integration: The AWS SDK offers pre-built libraries and APIs designed to integrate effortlessly with AWS services. Whether provisioning EC2 instances, managing S3 storage, or querying DynamoDB, the SDK simplifies these processes with clear, efficient code. 
Multi-Language Support: Supporting a range of programming languages, the SDK enables developers to work within their preferred coding environments. This flexibility facilitates AWS adoption across diverse teams and projects. 
Robust Security Features: Security is a fundamental aspect of the AWS SDK, with features such as automatic API request signing, IAM integration, and encryption options ensuring secure interactions with AWS services. 
High-Level Abstractions: To reduce repetitive coding, the SDK provides high-level abstractions for various AWS services. For instance, using Boto3, developers can interact with S3 objects directly without dealing with low-level request structures. 
Support for Asynchronous Operations: The SDK enables asynchronous programming, allowing non-blocking operations that enhance the performance and responsiveness of high-demand applications. 
Benefits of Using AWS SDK
Streamlined Development: By offering pre-built libraries and abstractions, the AWS SDK significantly reduces development overhead. Developers can integrate AWS services efficiently without navigating complex API documentation. 
Improved Reliability: Built-in features such as error handling, request retries, and API request signing ensure reliable and robust interactions with AWS services. 
Cost Optimization: The SDK abstracts infrastructure management tasks, allowing developers to focus on optimizing applications for performance and cost efficiency. 
Comprehensive Documentation and Support: AWS provides detailed documentation, tutorials, and code examples, catering to developers of all experience levels. Additionally, an active developer community offers extensive resources and guidance for troubleshooting and best practices. 
Common Use Cases
Cloud-Native Development: Streamline the creation of serverless applications with AWS Lambda and API Gateway using the SDK. 
Data-Driven Applications: Build data pipelines and analytics platforms by integrating services like Amazon S3, RDS, or Redshift. 
DevOps Automation: Automate infrastructure management tasks such as resource provisioning and deployment updates with the SDK. 
Machine Learning Integration: Incorporate machine learning capabilities into applications by leveraging AWS services such as SageMaker and Rekognition.
Conclusion 
The AWS Software Development Kit is an indispensable tool for developers aiming to fully leverage the capabilities of AWS services. With its versatility, user-friendly interface, and comprehensive features, it serves as a critical resource for building scalable and efficient applications. Whether you are a startup creating your first cloud-native solution or an enterprise seeking to optimize existing infrastructure, the AWS SDK can significantly streamline the development process and enhance application functionality. 
Explore the AWS SDK today to unlock new possibilities in cloud-native development.
0 notes
jatinu03 · 11 months ago
Text
Code
sudo tcpdump -lnvX icmp
sudo ip link add vxlan0 type vxlan id 100 dev eth0 local 10.0.1.11 dstport 4789 (This creartes the VXLAN inteface and decapsulates VXLAN traffic using the VXLAN Id supplied in step 5.4)
sudo ip link set vxlan0 up (This ebavles the VXLAN interface vxlan0)
sudo tcpdump -lnvXi vxlan0 icmp _(This captures traffic on the vxlan0)
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "ec2:CreateTags", "ec2:DeleteTags" ], "Resource": "arn:aws:ec2:::volume/*", "Condition": { "StringNotEqualsIfExists": { "aws:RequestTag/jam_testing": ["yes", "no"] }, "Null": { "aws:RequestTag/jam_testing": "false" } } } ] }
import boto3 from botocore.exceptions import ClientError
def create_table(dynamodb=None): if not dynamodb: dynamodb = boto3.resource('dynamodb') table = dynamodb.create_table( TableName='awsjam_employee', KeySchema=[ { 'AttributeName': 'employee_id', 'KeyType': 'HASH' # Partition key } ], AttributeDefinitions=[ { 'AttributeName': 'employee_id', 'AttributeType': 'N' } ], ProvisionedThroughput={ 'ReadCapacityUnits': 10, 'WriteCapacityUnits': 10 } ) return table
create_table()
Above---- touch dynamodb.py
touch insertrecord.py Below----
import boto3
def insert_record(table_name,employee_id, name, age, salary, city): dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('awsjam_employee') table.put_item( Item={ 'employee_id': employee_id, 'name': name, 'age': age, 'salary': salary, 'city': city } )
insert_record('awsjam_employee', 1, 'John', '30', '20000', 'New York') insert_record('awsjam_employee', 2, 'Peter', '25', '30000', 'New York') insert_record('awsjam_employee', 3, 'Mark', '35', '40000', 'New York') insert_record('awsjam_employee', 4, 'Mary', '40', '50000', 'New York') insert_record('awsjam_employee', 5, 'Jennifer', '19', '60000', 'New York')
import boto3 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('awsjam_employee') table.update_item( Key={ 'employee_id': 1 }, UpdateExpression='SET salary = :val1', ExpressionAttributeValues={ ':val1': 50000 } )
py import boto3 def delete_item(dynamodb=None): dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('awsjam_employee') response = table.scan() for item in response['Items']: if(int(item['age']) <= 20): resp = table.delete_item(Key={'employee_id': int(item['employee_id'])}) response = table.scan() print(response)
invoke function
delete_item()
aws evidently start-launch --launch SpecialSale --project OriginalGoodsStore --region us-east-2
select B.* from "$REPLACEWITHDATASOURCENAME"."default"."autocustomer" B join "AwsDataCatalog"."vehicles"."$REPLACEWITHDATATABLENAMECREATEDBYCRAWLER" A ON cast(A.policyid as integer)=B.policyid where A.title_status = 'clean vehicle' and A.year = '2007' and A.state='california' and A.brand='chrysler'
aws kinesis list-shards --stream-name *KinesisStreamForJam aws kinesis split-shard --stream-name *KinesisStreamForJam* --shard-to-split shardId-000000000002 --new-starting-hash-key *320854911280625642308916404954512140970 aws kinesis update-shard-count --stream-name *KinesisStreamForJam* --target-shard-count *8* --scaling-type **UNIFORM_SCALING 180
1 note · View note
katyslemon · 4 years ago
Text
CRUD Operations Using AWS DynamoDB with Flask APIs and Boto3
Introduction
In this tutorial, we will build a demo application together; learn about the Flask framework, Boto3 library, REST APIs, and AWS DynamoDB. If you are new at using AWS DynamoDB with Flask APIs and Boto3, don’t worry! We are here to help you learn through this tutorial.
Goal: Using AWS DynamoDB with Flask APIs and Boto3
Before knowing how to implement CRUD operations on DynamoDB using Flask APIs, let’s see the video and know what we will build.
Prerequisites
We assume that you have prior knowledge of the following:
Python3 Programming
pip
Flask
AWS and IAM
To get started with this tutorial, make sure you have the following as well:
DynamoDB local
Python
IDE
Technologies to use
We will be using the following technologies:
Flask
Boto3
DynamoDB
Let’s start the implementation of AWS DynamoDB with Flask APIs and Boto3. Here’s my system setup:
Ubuntu 20.04 OS
Python 3.8+
Postman
Installing Flask and Boto3 using pip
Install the packages to build REST APIs
Flask
Boto3
Read more for CRUD Operations Using AWS DynamoDB with Flask APIs and Boto3
0 notes
sakhshimandal · 2 years ago
Text
Extract Transform Load Python
Tumblr media
Extract, Transform, Load (ETL) is a process used in data integration and business intelligence that involves extracting data from multiple sources, transforming it to meet specific requirements, and then loading it into a target system. ETL is an essential process for any organization that wants to harness the power of their data, and Python is a popular language for building ETL pipelines.
In this article, we'll take a closer look at Extract Transform Load Python and how it can be used to build robust data pipelines.
Extracting Data with Python
The first step in an ETL pipeline is extracting data from one or more sources. Python has a wide range of libraries that can be used to extract data from various sources, including databases, APIs, files, and web scraping.
Some of the popular libraries for extracting data include:
pandas - pandas is a data manipulation library that provides easy-to-use data structures and data analysis tools for working with tabular and time-series data. pandas can read data from a variety of sources, including CSV, Excel, SQL databases, and HTML.
SQLAlchemy - SQLAlchemy is a popular SQL toolkit and Object-Relational Mapping (ORM) library that provides a high-level interface for interacting with databases in Python. SQLAlchemy supports a wide range of databases, including MySQL, PostgreSQL, SQLite, and Oracle.
Beautiful Soup - Beautiful Soup is a Python library that is used for web scraping to extract data from HTML and XML files. Beautiful Soup provides easy-to-use functions for parsing HTML and XML files and extracting the required data.
Transforming Data with Python
Once the data has been extracted, the next step is transforming it to meet the specific requirements of the target system. This involves cleaning, filtering, sorting, aggregating, and transforming the data into the required format.
Python provides a wide range of libraries for data transformation, including:
pandas - pandas provides a wide range of functions for data cleaning, filtering, sorting, and aggregation. pandas also provides functions for transforming data between different data types and formats.
NumPy - NumPy is a popular library for numerical computing in Python. NumPy provides a wide range of functions for mathematical operations on arrays, including vector and matrix operations.
SciPy - SciPy is a library for scientific computing in Python. SciPy provides a wide range of functions for optimization, integration, interpolation, signal processing, and statistics.
Loading Data with Python
Once the data has been extracted and transformed, the final step is loading it into the target system. Python provides a wide range of libraries for loading data into different target systems, including databases, files, and cloud services.
Some of the popular libraries for loading data include:
SQLAlchemy - SQLAlchemy provides a high-level interface for loading data into databases using SQL statements or ORM methods.
pandas - pandas provides functions for writing data to CSV, Excel, SQL databases, and other formats.
AWS SDK for Python (Boto3) - Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. Boto3 provides easy-to-use functions for loading data into AWS services, including Amazon S3, Amazon Redshift, and Amazon DynamoDB.
Conclusion
ETL Python is a powerful tool for building robust data pipelines for data integration and business intelligence. Python provides a wide range of libraries for extracting, transforming, and loading data from different sources, making it easy to build ETL pipelines that meet the specific requirements of your target system.
When building ETL pipelines with Python, it is important to choose the right libraries and tools for your specific use case. This will ensure that your ETL pipeline is efficient, scalable, and meets the performance requirements of your target system.
0 notes
greyssj · 3 years ago
Text
Spotify queue
Tumblr media
Spotify queue how to#
Spotify queue code#
Spotify queue Pc#
import requests import time import boto3.
Spotify queue code#
The code for this specific project is written in Python below. That post will give you the background necessary in getting a Spotify client ID, as well as setting up AWS DynamoDB, API Gateway, and Lambda. And if your answer is yes, please share this article with your friends and family to give us your support. The backend service runs as an AWS Lambda function, just like my Spotify now playing post. If your answer is no, you can ask anything via the contact forum section related to this article.
Spotify queue how to#
I hope you understand this article, How to Clear Queue on Spotify.
Spotify will add all songs to your queue if it’s an album.įinal remarks: How to Clear Queue on Spotify.
If you do, you can create a whole new music direction and revive your profile, and here’s how: When you start using a queue, you’ll find that adding new tracks is just as easy as removing them from the list.
Now your queue is empty and you can start over.
After selecting all the songs, click “Delete” at the bottom left corner.
Check all the songs you want to delete.
Click on the song that is playing and click on the three-line icon in the bottom right corner.
Open your Spotify and go to the now playing screen.
Spotify queue Pc#
How to Set Live Wallpapers on Windows 11 PC How to clear your Spotify queue located or through the mobile application But if you ever want to clear your Spotify queue-maybe you’ve added too many sad songs, or maybe your 90s grunge style has fallen for the day-you can always clear your Spotify queue, whether you’re on your computer or not. There is no limit to the number of songs you can add. To reorder the songs in your queue, click on a song and drag it up or down. In the Now Playing area at the bottom of the page, click on the queue icon. You can add songs to your queue by right-clicking or tapping on the three dots next to their title and selecting “Add to Queue” from the menu that opens. Go to and log in to your account if you aren't already. They look like three horizontal lines with a right-pointing triangle in the top left. The queue button in Spotify looks like a list of songs. To do this, you need to know where the queue button is. First of all, you need to know how to see all the songs that you have in Spotify queue. All the songs you have in the queue will be deleted and you can start adding the songs you want to listen to at that moment. Sure, you can delete your profile and start fresh, but wouldn’t it be better to clean up the clutter and just put down a few playlists of the music you love? Clearing your Spotify queue gives you a fresh start. If your answer is yes after reading the article, please share this article with your friends and family to support us.Īfter years of using Spotify, the music you listen to can stray further from your tastes. I hope you guys like this blog, How to Clear Queue on Spotify. Recently I updated this tutorial and will try my best so that you understand this guide. This tutorial is about How to Clear Queue on Spotify.
Tumblr media
0 notes
globalmediacampaign · 4 years ago
Text
Integrate your Amazon DynamoDB table with machine learning for sentiment analysis
Amazon DynamoDB is a non-relational database that delivers reliable performance at any scale. It’s a fully managed, multi-Region, multi-active database that provides consistent single-digit millisecond latency and offers built-in security, backup and restore, and in-memory caching. DynamoDB offers a serverless and event-driven architecture, which enables you to use other AWS services to extend DynamoDB capability. DynamoDB provides this capability using Kinesis Data Streams for DynamoDB and DynamoDB Streams with AWS Lambda. When you enable DynamoDB Streams on a DynamoDB table, it captures a time-ordered sequence of item-level modifications in the table and stores this information in a change log for up to 24 hours. Downstream AWS services can access these change logs and view the data items as they appeared before and after they were modified, in near-real time, using a Lambda function. This allows the DynamoDB table to integrate functionally for additional use cases like machine learning (ML), ad hoc queries, full text search, event alerting, and more, such as the following: Processing DynamoDB data with Apache Hive on Amazon EMR for data warehousing use cases Configuring AWS credentials using Amazon Cognito to authenticate access to DynamoDB tables for mobile application Using Amazon API Gateway and Lambda with DynamoDB for a front-end serverless architecture In this post, I show you how to integrate ML with Amazon DynamoDB using Amazon Comprehend to analyze sentiments on incoming product reviews. Serverless and event-driven architecture When you perform create, read, update, and delete (CRUD) operations on a DynamoDB table, DynamoDB Streams keep a 24-hour change log of all CRUD operations. The stream offers you four options of log attributes. For more information, see Enabling a Stream. Lambda functions process each incoming log event, extract the necessary information required for downstream services, and invoke said services, such as in the following situations: A function can loop back to a DynamoDB table to create or update the aggregate item, like a summary item or average of statistics in real time. You can use a function to send specific attributes to Amazon Elasticsearch Service (Amazon ES) for full text search use cases. For historical analysis or ad hoc queries, the function sends the updates to Amazon Simple Storage Service (Amazon S3) in optimized format through Amazon Kinesis Data Firehose, where you can run ad hoc queries or analytics using Amazon Athena You can use the recently announced option to export DynamoDB table data to Amazon S3 for the initial setup of data for analysis; however, you can’t use it for continuous read requirements. Additionally, using Kinesis Data Firehose for data export offers optimized Apache Parquet If you want to be updated on a specific event on the DynamoDB table like the deletion of a critical record, Lambda can notify you through Amazon Simple Notification Service (Amazon SNS). The following diagram illustrates this event-driven architecture. Using ML and analytics with the DynamoDB solution You can configure Amazon review analysis and analytics by running AWS CloudFormation on your own account. This solution uses a serverless event-driven architecture with automated steps. The solution checks the sentiment on the incoming Amazon product review, creates a product review summary based on the sentiment, and keeps updates in optimized format for future ad hoc queries and analytics. The following diagram illustrates this architecture. For this post, the solution uses the incoming Amazon product reviews by putting a review object in the S3 bucket in the form of JSON records. The S3 bucket publishes the s3:ObjectCreated:put event to Lambda by invoking the Lambda function, as specified in the bucket notification configuration. Lambda is meant for quick processing, so the solution limits the size of the product review object on Amazon S3 to smaller than 5 KB. If you want to run this solution in your account, follow these steps: Download the following artifacts: CloudFormation template – Part 1 CloudFormation template – Part 2 Sample gz file for testing – test.json.gz Deploy the CloudFormation Part 1 stack. Check the CloudFormation documentation for instructions. Once Part 1 stack deployment is complete, deploy the Part 2 stack. Once Part 2 stack deployment is complete, upload the sample gz file to the S3 bucket (-amazonreviewsbucket-*) with Server-side encryption option. Check Amazon S3 documentation for instructions. The CloudFormation Part 1 stack creates the following resources: A Glue database and table for cataloging, Two Lambda functions and associated permissions, Required roles and policies, CloudWatch LogStream and LogGroup, Kinesis Firehose for sending streams data to S3 for analysis, Two S3 buckets for incoming reviews and parquet output for analysis, DynamoDB table and associated Streams. The CloudFormation Part 2 stack imports the resources created and setup a managed policy. You can run the stacks on all regions where these services are supported. I tested it on US regions, North Virginia, Oregon and Ohio. Please note running this solution in your account will incur costs. The Lambda function amazon-review-processing runs and assumes the AWS Identity and Access Management (IAM) role created by AWS CloudFormation. The function reads the Amazon S3 events it receives as a parameter, determines where the review object is, reads the review object, and processes the records in the review object. The function breaks the incoming record into multiple records and DynamoDB items: a review item and product item (if it doesn’t already exist).  This allows you to retrieve an individual item based on the partition key and sort key. If you want all the product items, you can query items based on just the product partition key. The following screenshot shows product summary item. The following screenshot shows review items associated with the product. The following Lambda code add the review data to DynamoDB table when review file is uploaded to S3 bucket. import io import os import gzip import json import boto3 def get_records(session, bucket, key): """ Generator for the bucket and key names of each CloudTrail log file contained in the event sent to this function from S3. (usually only one but this ensures we process them all). :param event: S3:ObjectCreated:Put notification event :return: yields bucket and key names """ s3 = session.client('s3') response = s3.get_object(Bucket=bucket, Key=key) with io.BytesIO(response['Body'].read()) as obj: with gzip.GzipFile(fileobj=obj) as logfile: records = json.load(logfile) return records def handler(event, context): """ Checks for API calls with RunInstances, TerminateInstances, and DeleteDBInstance in CloudTrail. if found, send specific records to SQS for processing :return: 200, success if records process successfully """ session = boto3.session.Session() dynamodb = boto3.resource("dynamodb", region_name='us-east-1') table = dynamodb.Table('AmazonReviews') # Get the S3 bucket and key for each log file contained in the event for event_record in event['Records']: try: bucket = event_record['s3']['bucket']['name'] key = event_record['s3']['object']['key'] print('Loading Amazon Reviews file s3://{}/{}'.format(bucket, key)) records = get_records(session, bucket, key) print('Number of records in log file: {}'.format(len(records))) for record in records: response = table.get_item(Key={'pk': record['product_id'], 'sk': '2099-12-31#PRODUCTSUMMARY'}) if 'Items' not in response: table.put_item( Item={ 'pk': record['product_id'], 'sk': '2099-12-31#PRODUCTSUMMARY', 'marketplace': record['marketplace'], 'product_parent': record['product_parent'], 'product_title': record['product_title'], 'product_category': record['product_category'], } ) table.put_item( Item={ 'pk': record['product_id'], 'sk': record['review_date'] + '#' + record['review_id'], 'customer_id': record['customer_id'], 'star_rating': record['star_rating'], 'helpful_votes': record['helpful_votes'], 'total_votes': record['total_votes'], 'vine': record['vine'], 'verified_purchase': record['verified_purchase'], 'review_headline': record['review_headline'], 'review_body': record['review_body'] } ) except Exception as e: print (e) return {'Exception status': e} else: print("records processed successfully!!") return { 'statusCode': 200, 'body': json.dumps('records inserted successfully to DynamoDB!!') } After the review records are added to the DynamoDB table, the items enter the DynamoDB stream in real time with new and old images. The amazon_reviews_summary Lambda function captures the stream records from the stream and processes them one by one. This Lambda function has multiple responsibilities: Capture review text from the stream record and call Amazon Comprehend for sentimental analysis. Amazon Comprehend limits review strings to fewer than 5,000 characters, so the code truncates review text to 4,999 characters before calling the Amazon Comprehend sentiment API. Add the sentiment response for the review record and create a product review summary record with sentiment counts. Flatten the DynamoDB streaming JSON logs and add the record to the Kinesis Data Firehose delivery stream. Invoke the Firehose delivery stream put_record API for putting updates in the S3 bucket. The following Lambda code processes the incoming DynamoDB Streams for sentiment analysis and save it to S3 for analytics. import json import boto3 import os def convert_file(f): out = {} def convert(element, name=''): if type(element) is dict: for sub in element: convert(element[sub], name + sub + '_') elif type(element) is list: ctr = 0 for sub in element: convert(sub, name + str(ctr) + '_') ctr += 1 else: out[name[:-1]] = element convert(f) return out def handler(event, context): cmphd = boto3.client(service_name='comprehend', region_name='us-east-1') fh = boto3.client('firehose') ddb = boto3.resource('dynamodb', region_name='us-east-1') dt=ddb.Table('AmazonReviews') FIREHOSE_URL = os.environ['FIREHOSE_URL'] for rec in event['Records']: if (rec['eventName'] == 'INSERT' and ('review_body' in rec['dynamodb']['NewImage'])): convt=convert_file(rec) response = fh.put_record( DeliveryStreamName=FIREHOSE_URL, Record={'Data': json.dumps(convt)} ) review_body=rec['dynamodb']['NewImage']['review_body']['S'] review_body=review_body[:4999] pk=rec['dynamodb']['Keys']['pk']['S'] sk=rec['dynamodb']['Keys']['sk']['S'] res=cmphd.detect_sentiment(Text=review_body, LanguageCode='en') st=res['Sentiment'] try: d_response = dt.put_item( Item={'pk': pk, 'sk': sk + '#' + 'SENTIMENT', 'sentiment': st} ) if st == "POSITIVE": d_s_response = dt.update_item( Key={'pk': pk,'sk': '2099-12-31#REVIEWSUMMARY'}, UpdateExpression="set positive_sentiment_count= if_not_exists(positive_sentiment_count, :start) + :inc",ExpressionAttributeValues={':inc': 1,':start': 0},ReturnValues="UPDATED_NEW" ) elif st == "NEGATIVE": d_s_response = dt.update_item( Key={'pk': pk,'sk': '2099-12-31#REVIEWSUMMARY'}, UpdateExpression="set negative_sentiment_count= if_not_exists(negative_sentiment_count, :start) + :inc",ExpressionAttributeValues={':inc': 1,':start': 0},ReturnValues="UPDATED_NEW" ) elif st == "NEUTRAL": d_s_response = dt.update_item( Key={'pk': pk,'sk': '2099-12-31#REVIEWSUMMARY'}, UpdateExpression="set neutral_sentiment_count= if_not_exists(neutral_sentiment_count, :start) + :inc",ExpressionAttributeValues={':inc': 1,':start': 0},ReturnValues="UPDATED_NEW" ) elif st == "MIXED": d_s_response = dt.update_item( Key={'pk': pk,'sk': '2099-12-31#REVIEWSUMMARY'}, UpdateExpression="set mixed_sentiment_count= if_not_exists(mixed_sentiment_count, :start) + :inc",ExpressionAttributeValues={':inc': 1,':start': 0},ReturnValues="UPDATED_NEW" ) else: print("No sentiment value: " + st) except Exception as e: return {'Exception status': e} else: print("record processed successfully") The amazon_reviews_summary Lambda function calls the Amazon Comprehend detect_sentiment API with the review text, and Amazon Comprehend returns one of the four sentiments: positive, negative, neutral, or mixed. If you want more granular sentiments, you can replace Amazon Comprehend with your own ML model. The amazon_reviews_summary Lambda function calls the DynamoDB table to update the sentiment response on the review item and create product review summary items (or update them if they already exist). The following screenshot shows sentiment item associated with review. The following screenshot shows review summary item. The amazon_reviews_summary Lambda function calls the Firehose delivery stream to flatten the DynamoDB streaming JSON payload. The delivery stream converts the record format to Parquet and adds these records to the S3 bucket. Kinesis Firehose is configured with Record format conversion enabled and Output format as Apache Parquet. You can now catalog the S3 bucket on Athena and run ad hoc queries. See the following query and result: SELECT * FROM "amazon_review"."amazon_reviews" where dynamodb_keys_pk_s ='B00004U2JW' eventid,eventname,eventversion,eventsource,awsregion,dynamodb_approximatecreationdatetime,dynamodb_keys_sk_s,dynamodb_keys_pk_s,dynamodb_newimage_total_votes_n,dynamodb_newimage_star_rating_n,dynamodb_newimage_sk_s,dynamodb_newimage_verified_purchase_s,dynamodb_newimage_pk_s,dynamodb_newimage_customer_id_n,dynamodb_newimage_helpful_votes_n,dynamodb_newimage_vine_s,dynamodb_newimage_review_body_s,dynamodb_newimage_review_headline_s,dynamodb_sequencenumber,dynamodb_sizebytes,dynamodb_streamviewtype,eventsourcearn 35f3c7ce3083aa72eb6b39cd56f3681d,INSERT,1.1,aws:dynamodb,us-east-1,1598908834,2000-09-07#R3MA96SRZSSBER,B00004U2JW,27,4,2000-09-07#R3MA96SRZSSBER,N,B00004U2JW,48308880,5,N,"cool camera with functions whitch i do need, but I cant buy her /and others from my wish list/ because I am not living in U.S. Amazon hurry, she is one of my favourite...",cool but to long way to get her to me,8.53782E+25,393,NEW_AND_OLD_IMAGES,arn:aws:dynamodb:us-east-1:080931190378:table/amazon-reviews-v1/stream/2020-08-14T06:28:33.219 Cleaning up To avoid incurring future costs, delete the resources you created with the CloudFormation stack: Delete the contents of the review bucket and Parquet analytics bucket. Delete the Part 2 CloudFormation stack first and then delete the Part 1 CloudFormation stack. Conclusion In this post, I showed how you can integrate your DynamoDB table with Amazon Comprehend for sentiment analysis and Athena for ad hoc queries across historical activities. You can use your own ML model instead of Amazon Comprehend if you want more granular sentiment analysis. You can also use other AWS services to integrate with DynamoDB by using DynamoDB Streams to extend DynamoDB features. I welcome your feedback, suggestions or questions on this post, please leave me comments below. About the author Utsav Joshi is a Technical Account Manager at AWS. He lives in New Jersey and enjoys working with AWS customers to solve architectural, operational, and cost optimization challenges. In his spare time, he enjoys traveling, road trips, and playing with his kids. https://aws.amazon.com/blogs/database/integrate-your-amazon-dynamodb-table-with-machine-learning-for-sentiment-analysis/
0 notes
awsexchage · 6 years ago
Photo
Tumblr media
Amazon Managed BlockchainでHyperledger Fabricのブロックチェーンネットワークをさくっと構築するAWS CloudFormationのテンプレートを作ってみた(解説編) https://ift.tt/2M19PhQ
Amazon Managed Blockchain(AMB)でブロックチェーンネットワークを構築するのが手間になったので、AWS CloudFormation(CFn)を利用して構築できるようにしてみました。使い方は下記をご参考ください。
Amazon Managed BlockchainでHyperledger Fabricのブロックチェーンネットワークをさくっと構築するAWS CloudFormationのテンプレートを作ってみた(使い方編) – Qiita https://cloudpack.media/48077
Cfnのテンプレートを作成するのにいくつかハマったりしたので解説がてらまとめてみます。 テンプレートはGitHubにアップしています。
kai-kou/amazon-managed-blockchain-cfn-template https://github.com/kai-kou/amazon-managed-blockchain-cfn-template
CFnがAMBリソースに対応していない
AMBのネットワークやメンバーなどのリソースがCFnでサポートされていません。(2019/07/03時点)
AWS Resource and Property Types Reference – AWS CloudFormation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
Release History – AWS CloudFormation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ReleaseHistory.html
そのためAWS Lambda-backedカスタムリソースを利用してLambda関数でAWS SDKを利用してAMBのネットワークやメンバーを作成する必要があります。
詳細は下記が参考になります。
AWS SDK for Python(boto3)でAmazon Managed Blockchainのブロックチェーンネットワークを作成してみた – Qiita https://cloudpack.media/47241
AWS CloudFormationのLambda-backedカスタムリソースでリソースの更新・削除をする方法 – Qiita https://cloudpack.media/48205
リソースの作成順を制御する
AMBのPeerノードはネットワーク(メンバー)が利用可能になってから追加する必要があるため、CFnのDependsOn 属性でリソースの作成順を制御してネットワーク(→メンバー)→Peerノードの順にリソースを作成します。 ※ネットワークで最初のメンバーはネットワークと同時に作成する必要があります。
DependsOn 属性 – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html
AWS Lambda-backedカスタムリソースでリソース作成完了を待ち受ける
DependsOn 属性でリソースの作成順を制御することができたのですが、AMBのリソース(ネットワーク、Peerノード)をAWS CLIやAWS SDKで作成するとNetworkIdなどがすぐにレスポンスとして返ってきます。AMBだと「レスポンスが返ってきた=リソース作成できた」ではなく「レスポンスが返ってきた=リソース作成が開始された」となります。リソースが作成完了し利用可能になるには20分ほどかかります。
なので、CFnでネットワーク作成してすぐにPeerノードを作成しようとするとネットワークが利用可能になっておらずエラーになります。
そのためCFnでリソースの作成を待ち受けるのにAWS Lambda-backedカスタムリソースでどうにかする必要がありますが、AMBネットワークの作成には20分程度かかり、AWS Lmabdaのタイム��ウト(15分)を超えてしまうため、作成用のカスタムリソースだけでは待受けすることができません。 そこで、待受用のカスタムリソースを定義することで、AWS Lambdaのタイムタウト(15分) x リトライ回数(3回)で45分まで待ち受けられるようにします。詳細は下記が参考になります。
AWS CloudFormationのLambda-Backedカスタムリソースでリソース作成を待ち受けできるようにする – Qiita https://cloudpack.media/48222
このリソース作成を待ち受ける実装とDependsOn属性を利用することでAMBのリソースが作成できるようになりました。下記はCFnのテンプレートから抜粋したリソース定義となります。
テンプレート抜粋
Resources: # ネットワーク作成用のカスタムリソース CreateBlockchainNetwork: Type: Custom::CustomResource # リソース作成待受用のカスタムリソース BlockchainNetwork: Type: Custom::CustomResource DependsOn: CreateBlockchainNetwork # リソース情報取得用のカスタムリソース BlockchainMember: Type: Custom::CustomResource DependsOn: BlockchainNetwork # Peerノード作成用のカスタムリソース CreateBlockchainPeerNode: Type: Custom::CustomResource DependsOn: BlockchainMember # リソース作成待受用のカスタムリソース BlockchainPeerNode: Type: Custom::CustomResource DependsOn: CreateBlockchainPeerNode
AWS Lambdaで利用できるAWS SDKを最新にする
AMBは2019/05/01にGAとなったサービスです。
New – Amazon Managed Blockchain – Create & Manage Scalable Blockchain Networks | AWS News Blog https://aws.amazon.com/jp/blogs/aws/new-amazon-managed-blockchain-create-manage-scalable-blockchain-networks
AWS Lambdaの関数(Python)で利用できるAWS SDK(boto3 1.9.42)だとAMBが対応していないバージョンとなるため、AWS Lambda Layersを利用して最新のAWS SDK(boto3 1.9.139 以上)を利用する必要があります。(2019/07/03時点)
boto3/CHANGELOG.rst at develop · boto/boto3 https://github.com/boto/boto3/blob/develop/CHANGELOG.rst#19139
api-change:managedblockchain: [botocore] Update managedblockchain client to latest version
エラー例
Lambda関数の実装
import json import boto3 def lambda_handler(event, context): print(boto3.__version__) client = boto3.client("managedblockchain") return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
実行ログ
START RequestId: 1f574950-f61d-4d05-a2b6-a56e11eb2201 Version: $LATEST 1.9.42 [ERROR] UnknownServiceError: Unknown service: 'managedblockchain'. Valid service names are: acm, acm-pca, alexaforbusiness, apigateway, application-autoscaling, appstream, appsync, athena, autoscaling, autoscaling-plans, batch, budgets, ce, chime, cloud9, clouddirectory, cloudformation, cloudfront, cloudhsm, cloudhsmv2, cloudsearch, cloudsearchdomain, cloudtrail, cloudwatch, codebuild, codecommit, codedeploy, codepipeline, codestar, cognito-identity, cognito-idp, cognito-sync, comprehend, config, connect, cur, datapipeline, dax, devicefarm, directconnect, discovery, dlm, dms, ds, dynamodb, dynamodbstreams, ec2, ecr, ecs, efs, eks, elasticache, elasticbeanstalk, elastictranscoder, elb, elbv2, emr, es, events, firehose, fms, gamelift, glacier, glue, greengrass, guardduty, health, iam, importexport, inspector, iot, iot-data, iot-jobs-data, iot1click-devices, iot1click-projects, iotanalytics, kinesis, kinesis-video-archived-media, kinesis-video-media, kinesisanalytics, kinesisvideo, kms, lambda, lex-models, lex-runtime, lightsail, logs, machinelearning, macie, marketplace-entitlement, marketplacecommerceanalytics, mediaconvert, medialive, mediapackage, mediastore, mediastore-data, mediatailor, meteringmarketplace, mgh, mobile, mq, mturk, neptune, opsworks, opsworkscm, organizations, pi, pinpoint, pinpoint-email, polly, pricing, rds, redshift, rekognition, resource-groups, resourcegroupstaggingapi, route53, route53domains, s3, sagemaker, sagemaker-runtime, sdb, secretsmanager, serverlessrepo, servicecatalog, servicediscovery, ses, shield, signer, sms, snowball, sns, sqs, ssm, stepfunctions, storagegateway, sts, support, swf, transcribe, translate, waf, waf-regional, workdocs, workmail, workspaces, xray (略)
AWS Lambda Layersを利用して最新のAWS SDKを利用する方法は下記が参考になります。
AWS CloudFormationのAWS Lambda-backedカスタムリソースで最新のAWS SDKを利用する – Qiita https://cloudpack.media/48058
AMBリソースの更新・削除に対応する
AMBリソースはAWS Lambda-backedカスタムリソースで作成しているので、更新や削除もLambda-backedカスタムリソースで行う必要があります。
詳細は下記が参考になりますが、こちらもリソース作成の待受と同じく、作成とは別のカスタムリソースを定義する必要があります。
AWS CloudFormationのLambda-backedカスタムリソースでリソースの更新・削除をする方法 – Qiita https://cloudpack.media/48205
作成待ちと更新・削除を担うカスタムリソースは共通化できたので、最終的には下記のようなリソース定義となりました。
テンプレート抜粋
Resources: # ネットワーク作成用のカスタムリソース CreateBlockchainNetwork: Type: Custom::CustomResource # リソース作成待受とリソース情報取得・更新・削除用のカスタムリソース BlockchainNetwork: Type: Custom::CustomResource Properties: NetworkId: !GetAtt CreateBlockchainNetwork.NetworkId DependsOn: CreateBlockchainNetwork # リソース情報取得用のカスタムリソース BlockchainMember: Type: Custom::CustomResource Properties: NetworkId: !GetAtt BlockchainNetwork.Network.Id MemberId: !GetAtt CreateBlockchainNetwork.MemberId DependsOn: BlockchainNetwork # Peerノード作成用のカスタムリソース CreateBlockchainPeerNode: Type: Custom::CustomResource Properties: NetworkId: !GetAtt BlockchainNetwork.Network.Id MemberId: !GetAtt BlockchainMember.Member.Id DependsOn: BlockchainMember # リソース作成待受とリソース情報取得・更新・削除用のカスタムリソース BlockchainPeerNode: Type: Custom::CustomResource Properties: NetworkId: !GetAtt BlockchainNetwork.Network.Id MemberId: !GetAtt BlockchainMember.Member.Id NodeId: !GetAtt CreateBlockchainPeerNode.NodeId DependsOn: CreateBlockchainPeerNode
AWS lambda-backedカスタムリソースで返すリソース情報(JSON)に気をつける
AWS lambda-backedカスタムリソースのLambda関数ではcfnresponse.send(event, context, cfnresponse.SUCCESS, data)のようにして処理結果をCFnに返すことができます。
最後のパラメータdataはJSONとなり、CFnのFn::GetAttで参照可能です。
カスタムリソースの応答オブジェクト – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html
応答で送信される、custom resource providerによって定義された名前と値のペア。ここで指定する値には、Fn::GetAtt を使用して、テンプレート内の名前でアクセスできます。
ただし、ネストされたJSONをdataに含めてもNetwork.Idのようにして参照できないため、AWS SDKでAMBのリソース情報を取得して得られるJSONをそのままでは返すことができませんでした。
AWS CloudFormationのLambda-BackedカスタムリソースでネストされたJSONを返しても参照できない – Qiita https://cloudpack.media/48318
なので、AWS SDKでAMBのリソース情報を取得して得られるJSONを加工する必要があります。 JSONのキーをNetwork.Idとすると、CFn側でもNetwork.Idと参照できるようにしています。
LambdaからCFnにネットワーク情報を返す例
import cfnresponse import boto3 import json from datetime import date, datetime def handler(event, context): client = boto3.client("managedblockchain") networkId = event['ResourceProperties']['NetworkId'] response = {} if event['RequestType'] == 'Create': network = client.get_network( NetworkId=networkId ) orderingServiceEndpoint = network['Network']['FrameworkAttributes']['Fabric']['OrderingServiceEndpoint'] vpcEndpointServiceName = network['Network']['VpcEndpointServiceName'] response = { "Network.Id": networkId, "Network.FrameworkAttributes.Fabric.OrderingServiceEndpoint": orderingServiceEndpoint, "Network.VpcEndpointServiceName": vpcEndpointServiceName } cfnresponse.send(event, context, cfnresponse.SUCCESS, response)
詳細は下記が参考になります。
AWS CloudFormationのLambda-BackedカスタムリソースでネストされてるっぽいJSONを返す方法 – Qiita https://cloudpack.media/48329
Hyperledger FabricのクライアントをEC2インスタンスで構築する
Hyperledger FabricのクライアントをEC2インスタンスで構築する定義は下記を参考にさせてもらいました。
awslabs/amazon-managed-blockchain-client-templates: AWS CloudFormation templates to provision Amazon EC2 instances and install and configure clients for use with blockchain frameworks in Amazon Managed Blockchain https://github.com/awslabs/amazon-managed-blockchain-client-templates
こちらはHyperledger FabricのクライアントとなるEC2インスタンスを作成するテンプレートでしたので、自前のテンプレートへ組み込み、AMBで必要となるリソースの作成後、インスタンスが作成されるようにしました。
ポイントとしては以下となります。
CFnのcfn-signalヘルパースクリプトで完了シグナルをCFnに返す
CFnでEC2インスタンスを作成すると、EC2インスタンスのステータスがRunningとなった時点でリソース作成完了となります。 そのため、テンプレートのUserDataで定義しているコマンド実行でエラーとなっても正常完了扱いとなり不便だったので、CFnのcfn-signalヘルパースクリプトで完了シグナルをCFnに返すようにしました。
cfn-signalを利用するには、CreationPolicyを定義する必要があります。
EC2インスタンス定義_一部抜粋
BlockchainClient: Type: AWS::EC2::Instance UserData: Fn::Base64: Fn::Sub: - | #!/bin/bash (略) /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource BlockchainClient --region ${AWS::Region} CreationPolicy: ResourceSignal: Timeout: PT20M
詳細は下記が参考になります。
cfn-signal – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/cfn-signal.html
Fn::Subで変数の取扱い
UserDataに指定する値はFn::Base64とFn::Subを用いて指定しています。 Fn::Subは文字列中に置き換えたい変数がある場合に利用する関数となっています。
Fn::Sub – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html
テンプレートで、スタックを作成または更新するまで使用できない値を含むコマンドまたは出力を作成するために、この関数を使用できます。
UserDataを編集しているとシェル変数を利用したくなるケースがでてきますが、そのまま利用するとエラーとなるため注意が必要です。シェル変数の利用方法は下記が参考になりました。
CloudFormationの中のEC2のユーザーデータでシェル変数を使用する | DevelopersIO https://dev.classmethod.jp/cloud/aws/using-variables-in-ec2-user-data-in-cloudformation/
UserDataで変数を利用する例
UserData: Fn::Base64: Fn::Sub: - | #!/bin/bash # これはOK echo ${HOGE} HOGE2=hogehoge # これはエラーになる echo ${HOGE2} # こうするとOK echo ${!HOGE2} - { HOGE: hoge }
Hyperledger Fabricのcliでコマンドを実行するタイミングに気をつける
以下は、UserDataの後半部分の抜粋となります。ところどころでsleepコマンドを実行して待受けています。 リソース作成を繰り返し試行錯誤した結果となりますが、主に下記の理由からとなります。
Peerノードが利用可能になるのを待ち受け
OrdererからPeerノードへのデータ送信待ち
UserData一部抜粋
(略) /usr/local/bin/docker-compose -f docker-compose-cli.yaml up -d sleep 5m # enroll fabric-ca-client enroll -u https://${ADMIN_USERNAME}:${ADMIN_PASSWORD}@${FABRIC_CA_ENDPOINT} --tls.certfiles /home/ec2-user/${FABRIC_CA_FILE} -M /home/ec2-user/admin-msp cp -r /home/ec2-user/admin-msp/signcerts /home/ec2-user/admin-msp/admincerts echo ' Organizations: (略) ' > /home/ec2-user/configtx.yaml docker exec cli configtxgen -outputCreateChannelTx /opt/home/mychannel.pb -profile OneOrgChannel -channelID mychannel --configPath /opt/home/ sleep 30s # Create Channel docker exec cli peer channel create -c mychannel -f /opt/home/mychannel.pb -o ${ORDERING_SERVICE_ENDPOINT} --cafile /opt/home/${FABRIC_CA_FILE} --tls sleep 30s docker exec cli peer channel join -b mychannel.block -o ${ORDERING_SERVICE_ENDPOINT} --cafile /opt/home/${FABRIC_CA_FILE} --tls sleep 30s # Install ChainCode docker exec cli peer chaincode install -n mycc -v v0 -p github.com/chaincode_example02/go docker exec cli peer chaincode instantiate -o ${ORDERING_SERVICE_ENDPOINT} -C mychannel -n mycc -v v0 -c '{"Args":["init","a","100","b","200"]}' --cafile /opt/home/${FABRIC_CA_FILE} --tls sleep 30s docker exec cli peer chaincode list --instantiated -o ${ORDERING_SERVICE_ENDPOINT} -C mychannel --cafile /opt/home/${FABRIC_CA_FILE} --tls /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource BlockchainClient --region ${AWS::Region}
UserData の実行ログの確認方法
UserDataで指定したコマンドの実行ログが確認できないか調べてみたらしっかりと出力されていました。 下記が参考になりました。
Linux インスタンスでの起動時のコマンドの実行 – Amazon Elastic Compute Cloud https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/user-data.html
AWSのCloud initのログの場所 | しびら http://yamada.daiji.ro/blog/?p=191
EC2インスタンス内
$ cat /var/log/cloud-init-output.log (略) + docker exec cli peer channel join -b mychannel.block -o orderer.n-xxxxxxxxxxxxxxxxxxxxxxxxxx.managedblockchain.us-east-1.amazonaws.com:30001 --cafile /opt/home/managedblockchain-tls-chain.pem --tls 2019-07-01 09:17:03.663 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2019-07-01 09:17:03.903 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel + sleep 30s + docker exec cli peer chaincode install -n mycc -v v0 -p github.com/chaincode_example02/go 2019-07-01 09:17:34.076 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-07-01 09:17:34.076 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2019-07-01 09:17:34.490 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > + sleep 30s + docker exec cli peer chaincode instantiate -o orderer.n-xxxxxxxxxxxxxxxxxxxxxxxxxx.managedblockchain.us-east-1.amazonaws.com:30001 -C mychannel -n mycc -v v0 -c '{"Args":["init","a","100","b","200"]}' --cafile /opt/home/managedblockchain-tls-chain.pem --tls 2019-07-01 09:17:44.686 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2019-07-01 09:17:44.686 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc + sleep 30s + docker exec cli peer chaincode list --instantiated -o orderer.n-xxxxxxxxxxxxxxxxxxxxxxxxxx.managedblockchain.us-east-1.amazonaws.com:30001 -C mychannel --cafile /opt/home/managedblockchain-tls-chain.pem --tls Get instantiated chaincodes on channel mychannel: Name: mycc, Version: v0, Path: github.com/chaincode_example02/go, Escc: escc, Vscc: vscc + /opt/aws/bin/cfn-signal -e 0 --stack amb-cfn-test --resource BlockchainClient --region us-east-1 Cloud-init v. 0.7.6 finished at Mon, 01 Jul 2019 09:19:16 +0000. Datasource DataSourceEc2. Up 692.70 seconds
まとめ
AMBのリソースをCFnで管理するのにAWS Lambda-backedカスタムリソースを利用することができましたが、そこそこハマるところがあり、テンプレート作成に時間がかかりました。 1度作成できたら応用を効かせることができそうですので、個人的には良いテンプレートができたなと思ってます^^
参考
Amazon Managed BlockchainでHyperledger Fabricのブロックチェーンネットワークをさくっと構築するAWS CloudFormationのテンプレートを作ってみた(使い方編) – Qiita https://cloudpack.media/48077
kai-kou/amazon-managed-blockchain-cfn-template https://github.com/kai-kou/amazon-managed-blockchain-cfn-template
AWS Resource and Property Types Reference – AWS CloudFormation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html
Release History – AWS CloudFormation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/ReleaseHistory.html
AWS SDK for Python(boto3)でAmazon Managed Blockchainのブロックチェーンネットワークを作成してみた – Qiita https://cloudpack.media/47241
AWS CloudFormationのLambda-backedカスタムリソースでリソースの更新・削除をする方法 – Qiita https://cloudpack.media/48205
DependsOn 属性 – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html
AWS CloudFormationのLambda-Backedカスタムリソースでリソース作成を待ち受けできるようにする – Qiita https://cloudpack.media/48222
New – Amazon Managed Blockchain – Create & Manage Scalable Blockchain Networks | AWS News Blog https://aws.amazon.com/jp/blogs/aws/new-amazon-managed-blockchain-create-manage-scalable-blockchain-networks
boto3/CHANGELOG.rst at develop · boto/boto3 https://github.com/boto/boto3/blob/develop/CHANGELOG.rst#19139
AWS CloudFormationのAWS Lambda-backedカスタムリソースで最新のAWS SDKを利用する – Qiita https://cloudpack.media/48058
カスタムリソースの応答オブジェクト – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/crpg-ref-responses.html
AWS CloudFormationのLambda-BackedカスタムリソースでネストされたJSONを返しても参照できない – Qiita https://cloudpack.media/48318
AWS CloudFormationのLambda-BackedカスタムリソースでネストされてるっぽいJSONを返す方法 – Qiita https://cloudpack.media/48329
awslabs/amazon-managed-blockchain-client-templates: AWS CloudFormation templates to provision Amazon EC2 instances and install and configure clients for use with blockchain frameworks in Amazon Managed Blockchain https://github.com/awslabs/amazon-managed-blockchain-client-templates
cfn-signal – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/cfn-signal.html
Fn::Sub – AWS CloudFormation https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html
CloudFormationの中のEC2のユーザーデータでシェル変数を使用する | DevelopersIO https://dev.classmethod.jp/cloud/aws/using-variables-in-ec2-user-data-in-cloudformation/
Linux インスタンスでの起動時のコマンドの実行 – Amazon Elastic Compute Cloud https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/user-data.html
AWSのCloud initのログの場所 | しびら http://yamada.daiji.ro/blog/?p=191
元記事はこちら
「Amazon Managed BlockchainでHyperledger Fabricのブロックチェーンネットワークをさくっと構築するAWS CloudFormationのテンプレートを作ってみた(解説編))
August 05, 2019 at 04:00PM
0 notes
brokenbot2 · 6 years ago
Quote
こんな情報を見つけたよ AWS LambdaからPython Boto3を使用してDynamoDBを操作する https://t.co/w9Eh4L0NLF— 壊れbot 2号 (@broken_bot_2) October 11, 2019 October 12, 2019 at 06:12AM
http://twitter.com/broken_bot_2/status/1182766043749597185
0 notes
akrnd085 · 1 year ago
Text
Boto3 and DynamoDB: Integrating AWS’s NoSQL Service with Python
Tumblr media
Introduction Amazon Web Services aws boto3 dynamodb as an versatile NoSQL database service while Boto3 serves, as the Python based AWS SDK. This article aims to delve into the usage of Boto3 for interacting with DynamoDB encompassing operations recommended practices and typical scenarios. It will be complemented by Python code snippets.
Overview of Boto3 and DynamoDB Boto3 stands as an AWS Software Development Kit (SDK) specifically designed for Python programmers. Its purpose is to empower software developers with the ability to utilize services such as Amazon S3 and DynamoDB.
DynamoDB on the hand is a key value and document oriented database that excels in delivering exceptional performance within milliseconds regardless of scale. As a managed database solution it offers region functionality, with multiple masters while ensuring durability.
Setting Up Boto3 for DynamoDB Before you start, you need to install Boto3 and set up your AWS credentials:
1. Install Boto3:
pip install boto3 2. Configure AWS Credentials: Set up your AWS credentials in `~/.aws/credentials`:
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY Basic Operations with DynamoDB 1. Creating a DynamoDB Table: import boto3
dynamodb = boto3.resource(‘dynamodb’)
table = dynamodb.create_table(
TableName=’MyTable’,
KeySchema=[
{‘AttributeName’: ‘id’, ‘KeyType’: ‘HASH’}
],
AttributeDefinitions=[
{‘AttributeName’: ‘id’, ‘AttributeType’: ‘S’}
],
ProvisionedThroughput={‘ReadCapacityUnits’: 1, ‘WriteCapacityUnits’: 1}
)
table.wait_until_exists() 2. Inserting Data into a Table:
table.put_item(
Item={
‘id’: ‘123’,
‘title’: ‘My First Item’,
‘info’: {‘plot’: “Something interesting”, ‘rating’: 5}
}
) 3. Reading Data from a Table:
response = table.get_item(
Key={‘id’: ‘123’}
)
item = response[‘Item’]
print(item) 4. Updating Data in a Table: table.update_item(
Key={‘id’: ‘123’},
UpdateExpression=’SET info.rating = :val1',
ExpressionAttributeValues={‘:val1’: 6}
) 5. Deleting Data from a Table:
table.delete_item(
Key={‘id’: ‘123’}
) 6. Deleting a Table:
table.delete() Best Practices for Using Boto3 with DynamoDB
1. Manage Resources Efficiently: Always close the connection or use a context manager to handle resources.
2. Error Handling: Implement try-except blocks to handle potential API errors.
3. Secure Your AWS Credentials: Never hard-code credentials. Use IAM roles or environment variables.
Advanced Operations – Batch Operations: Boto3 allows batch operations for reading and writing data.
– Query and Scan: Utilize DynamoDB’s `query` and `scan` methods for complex data retrieval.
Use Cases for Boto3 and DynamoDB – Web Applications: Store and retrieve user data for web applications.
– IoT Applications: Capture and store IoT device data.
– Data Analysis: Store large datasets and retrieve them for analysis.
Monitoring and Optimization Use AWS CloudWatch to monitor the performance of your DynamoDB tables and optimize provisioned throughput settings as necessary.
Conclusion Boto3 provides a powerful and easy way to interact with AWS DynamoDB using Python. By understanding these basic operations and best practices, developers can effectively integrate DynamoDB into their applications.
0 notes
softshareblog-blog · 7 years ago
Text
AWS 上用 Python 和 Boto3 實施 RDS PostgreSQL 和 DynamoDB CRUD
AWS 上用 Python 和 Boto3 實施 RDS PostgreSQL 和 DynamoDB CRUD
使用 Python API – Boto3和 psycopg2 在 AWS 上實施 RDS PostgreSQL CRUD 和 DynamoDB! 用程式碼建構基礎架構 ! (more…)
View On WordPress
0 notes
simondnet · 8 years ago
Text
Import CSV Directly into DynamoDB
If you want to import directly into DynamoDB, and have the AWS CLI already configured, here’s a modification of the previous Python:
Source
#CSV import format #  AAPL, Apple #  AMD, AMD import boto3 import csv # Get the service resource. dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('stocks') filename = 'data.csv' with open(filename) as csvfile:        reader = csv.reader(csvfile, delimiter=',')        for line in reader:                print(line[0])                print(line[1])                table.put_item(Item={'id':line[0],'description':line[1]})
0 notes
globalmediacampaign · 4 years ago
Text
The 20 most-visited Amazon DynamoDB documentation pages
Over the last 6 months, customers visited the following 20 Amazon DynamoDB documentation pages more than all others (the list is ordered from the most-visited page to the least). Use this list to see what other AWS customers have been reading and decide what to learn next. What Is Amazon DynamoDB? This brief introduction to DynamoDB also serves as the welcome page in the DynamoDB Developer Guide. Working with Queries in DynamoDB The DynamoDB Query operation finds items based on primary key values. You can query any table or secondary index that has a composite primary key (a partition key and a sort key). Setting Up DynamoDB Local (Downloadable Version) The downloadable version of DynamoDB lets you write and test applications without accessing the DynamoDB web service. When you’re ready to deploy your application in production, you can make a few minor changes to the code so that it uses the DynamoDB web service. Core Components of Amazon DynamoDB In DynamoDB, tables, items, and attributes are the core components that you work with. A table is a collection of items, and each item is a collection of attributes. Using Global Secondary Indexes in DynamoDB Some applications might need to perform many kinds of queries, using a variety of different attributes as query criteria. To support these requirements, you can create one or more global secondary indexes and issue Query requests against those indexes. Query Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation returns all the items from the table or index with that partition key value. Best Practices for Designing and Architecting with DynamoDB Quickly find recommendations for maximizing performance and minimizing throughput costs when working with DynamoDB. Getting Started Developing with Python and DynamoDB Learn how to use the AWS SDK for Python (Boto3) to write simple programs to perform DynamoDB operations. Getting Started with Node.js and DynamoDB Learn how to use the AWS SDK for JavaScript to write simple applications to perform DynamoDB operations. Getting Started with DynamoDB and AWS SDKs Language-specific, hands-on tutorials to help you learn about DynamoDB. The sample code in these tutorials can run against either DynamoDB local or the DynamoDB web service. Change Data Capture for DynamoDB Streams DynamoDB Streams captures a time-ordered sequence of item-level modifications in any DynamoDB table and stores this information in a log for up to 24 hours. Applications can access this log and view the data items as they appeared before and after they were modified, in near-real time. Read/Write Capacity Mode DynamoDB has two read/write capacity modes for processing reads and writes on your tables: on-demand and provisioned. NoSQL Workbench for DynamoDB NoSQL Workbench is a cross-platform client-side GUI application for database development and operations, and it’s available for Windows, macOS, and Linux. NoSQL Workbench is an integrated development environment tool that provides data modeling, data visualization, and query development features to help you design, create, query, and manage DynamoDB tables. Condition Expressions To manipulate data in a DynamoDB table, use the PutItem, UpdateItem, and DeleteItem operations. For these data manipulation operations, you can specify a condition expression to determine which items should be modified. Update Expressions An update expression specifies how UpdateItem modifies the attributes of an item—for example, setting a scalar value or removing elements from a list or a map. Working with Items and Attributes In DynamoDB, an item is a collection of attributes, and each attribute has a name and a value. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality. Improving Data Access with Secondary Indexes A secondary index is a data structure that contains a subset of attributes from a table, along with an alternate key to support Query operations. You can retrieve data from the index using Query, in much the same way as you use Query with a table. A table can have multiple secondary indexes, which give your applications access to many different query patterns. UpdateItem Use this action to edit an existing item’s attributes, or add a new item to a table if it doesn’t already exist. You can put, delete, or add attribute values. Working with Scans in DynamoDB A Scan operation in DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them. Amazon DynamoDB: How It Works Learn about the DynamoDB service components and how they interact. Let us know how we can make these DynamoDB documentation pages more useful to you by reaching out to the @DynamoDB Twitter handle. You also can choose Provide feedback at the bottom of any of these pages. About the author Craig Liebendorfer is a senior technical editor at Amazon Web Services. He also runs the @DynamoDB Twitter handle. https://aws.amazon.com/blogs/database/the-20-most-visited-amazon-dynamodb-documentation-pages/
0 notes
globalmediacampaign · 4 years ago
Text
Implementing version control using Amazon DynamoDB
Some applications require you to record changes to data over time, and you can identify these changes by a timestamp or number so it’s possible to retrieve specific versions later. In addition, you should be able to easily retrieve the most recent version and the design of the application to maintain data integrity as versions consistently increase. This post covers how to design and implement time-based and number-based version control in Amazon DynamoDB. A composite primary key is used for all four examples to model historical versions of data and to enable easy retrieval of the most recent version of data. You can find a Python implementation of the following solutions in the GitHub repo. Time-based versioning When data changes occur in sequence over time, you can use a timestamp to version data whenever it accompanies the changed data. For example, perhaps you need to design a data model for a factory that has many parts, and each part sends state data every 2 minutes. You’re required to store the historical state of each part in the database. The following data model illustrates how you could model this data in DynamoDB. Each state data (1) is added to the equipment item collection, and the sort key holds the timestamp accompanied by the state data. The Metadata item (2) acts as metadata for an equipment entity, containing attributes specific to this entity, such as Name, FactoryID, and LineID. This item is rarely updated. The following code illustrates the Python implementation of how to store and retrieve the latest state data for an equipment item: # Example state data event = { 'ID': 'Equipment#1', 'State': 'WARNING1', 'Time': '2020-11-12T20:04:00' } ddb = boto3.resource('dynamodb') table = ddb.Table('VersionControl') # Add the new state data item table.put_item( Item={ 'PK': event['ID'], 'SK': event['Time'], 'State': event['State'] } ) # Retrieve the latest state data response = table.query( KeyConditionExpression = Key('PK').eq(event['ID']) & Key('SK').begins_with('2'), # Strongly consistent read ConsistentRead = True, # Sort items in descending order ScanIndexForward = False, # Specifies the maximum number of items to evaluate Limit = 1 ) items = response['Items'] The data type of the sort key attribute is String, which means that items in an item collection are sorted in order of UTF-8 bytes. When running a query operation, the results are returned by default in ascending order. In the preceding code, to reverse the order, the ScanIndexForward parameter is set to false, meaning that the item with Metadata as sort key is the first returned item, followed by the items with timestamp as sort key, from the latest to the earliest. To retrieve only the item with the latest date in the sort key, you use the begins_with() function in the KeyConditionExpression, as well as Limit, which specifies the maximum number of items to evaluate. Number-based versioning In contrast, some applications require a number-based version that gets updated to the next higher version after each change to the data, even though a timestamp might be available. To model historical versions of data and easily retrieve the most recent version of data, you can use a composite primary key for your DynamoDB table and follow the version control design pattern using sort key prefixes. Let’s assume you need to use a number-based version instead of a time-based version for our factory use case, where you are required to store the historical state of each equipment in the database. The following data model illustrates how you can model this data in DynamoDB. The item with the Metadata sort key (1) acts as metadata for an equipment entity, containing attributes specific to the entity, such as Name, FactoryID, and LineID. This item is rarely updated. The item with v0 in the sort key (2) holds a copy of the latest equipment revision, including all of its attributes, and also the Latest attribute, which holds the latest version number. This item is always updated with the content of the most recent version. Every time the equipment is updated, a new item with the next higher version in the sort key and the updated contents is added (3). If your data model requires it, a sort key can include additional data after the version prefix. Implementing using atomic counters Depending on how an application is designed, the version number of the data might or might not be supplied in the update request. When a version is not supplied, you can use atomic counters to increment the version number; in that case, there is no need to first retrieve the value of the Latest attribute from the table. To add the new revision item, set the ReturnValues parameter to UPDATED_NEW for the update operation, retrieve the value of the Latest attribute from the response, and pass it to the put operation as part of the sort key value. You can’t use a transactional write in this case because you have to retrieve the value of the Latest attribute from the response of the update operation and pass it to the put operation. Although this implementation works well, it’s obvious that it’s not idempotent. If the update operation fails, retry both the update and put operations together. If the update operation succeeds and the put operation fails, retry only the put operation to avoid re-incrementing the version in the update operation. Configure the retry mode and maximum attempts settings in the AWS SDK according to your application requirements; more retry attempts means that the function takes longer to respond in case of failure. For example, the AWS Lambda function running this code takes more time to complete and clients must wait longer for a response. Time-critical or latency-sensitive applications may prefer to fail without retrying, or to handle the error elsewhere. See the following code: # Example state data event = { 'ID': 'Equipment#1', 'State': 'WARNING1', 'Time': '2020-11-01T20:04:00' } ddb = boto3.resource('dynamodb') table = ddb.Table('VersionControl') # Update the item that contains the latest version and content response = table.update_item( Key={ 'PK': event['ID'], 'SK': 'v0' }, # Atomic counter is used to increment the latest version UpdateExpression='SET Latest = if_not_exists(Latest, :defaultval) + :incrval, #time = :time, #state = :state', ExpressionAttributeNames={ '#time': 'Time', '#state': 'State' }, ExpressionAttributeValues={ ':time': event['Time'], ':state': event['State'], ':defaultval': 0, ':incrval': 1 }, # return the affected attribute after the update ReturnValues='UPDATED_NEW' ) # Get the updated version latest_version = response['Attributes']['Latest'] # Add the new item with the latest version table.put_item( Item={ 'PK': event['ID'], 'SK': 'v' + str(latest_version), 'Time': event['Time'], 'State': event['State'] } ) Implementing using DynamoDB Streams, Lambda functions, and atomic counters You can use the change data capture feature in DynamoDB to simulate a transactional write, including the update and put operations, and at the same time decouple these two operations to make the error handling non-blocking. You can use either Amazon Kinesis Data Streams for DynamoDB or DynamoDB Streams in the implementation; the following diagram and code show how you can implement version control with DynamoDB Streams and Lambda. Because each Lambda function only has one responsibility, the error handling is simpler and can be configured on the Lambda event source mapping, for example using BisectBatchOnFunctionError. When you configure DynamoDB Streams for your table in DynamoDB, choose NEW_IMAGE or NEW_AND_OLD_IMAGES for StreamViewType based on what your use case needs. The following code is the Lambda function that writes the latest version to the DynamoDB table: # Lambda function that writes the latest version to DynamoDB table def handler(event, _): ddb = boto3.resource('dynamodb') table = ddb.Table('VersionControl') # Update the item that contains the latest version and content table.update_item( Key={ 'PK': event['ID'], 'SK': 'v0' }, # Atomic counter is used to increment the latest version UpdateExpression= 'SET Latest = if_not_exists(Latest, :defaultval) + :incrval, #time = :time, #state = :state', ExpressionAttributeNames={ '#time': 'Time', '#state': 'State' }, ExpressionAttributeValues={ ':time': event['Time'], ':state': event['State'], ':defaultval': 0, ':incrval': 1 } ) When the update operation on the item with v0 in the sort key is performed successfully, a record containing the updated item including the Latest attribute is available in the DynamoDB stream and can be read by the Lambda function. In case of failure after maximum retry attempts in the put operation or in the Lambda function, you can configure a dead-letter queue (DLQ) for the failed records to be manually processed later. This way, you ensure that the sequence of versions is saved and you can later add this specific version to the table. For more information, see Error handling. The following code is the Lambda function that processes items from DynamoDB Streams: # Lambda function that processes items from DynamoDB Stream def handler(event, _): ddb = boto3.resource('dynamodb') table = ddb.Table('VersionControl') for record in event['Records']: if record['dynamodb']['Keys']['SK']['S'] == 'v0': new_image = record['dynamodb']['NewImage'] latest_id = new_image['PK']['S'] latest_version = new_image['Latest']['N'] latest_time = new_image['Time']['S'] latest_state = new_image['State']['S'] # Add the new item with the latest version table.put_item( Item={ 'PK': latest_id, 'SK': 'v' + str(latest_version), 'Time': latest_time, 'State': latest_state } ) Using DynamoDB Streams provides a flexible solution with independent error handling, but it comes with the additional cost of DynamoDB Streams and Lambda invocations. There is also a delay between the update and put operations towards the table. Implementing using TransactionalWrite To ensure the accuracy and consistency of the version control, this implementation uses the optimistic concurrency control method as well as transactional writes, where update and put actions are grouped in a single TransactWriteItems operation that either succeeds or fails as a unit. This implementation is suitable for applications that can’t tolerate inconsistent versioning, or delays between the update and put operations. However, transactional operations consume more WCUs than the standard put and update operations. The optimistic concurrency control method protects the data from being overwritten by the writes of others. Let’s assume that the state of the equipment is changing every second and the application is required to store these changes in the table. You either know what the latest version number for this specific equipment is, or you need to retrieve it from the item with v0 in the sort key. By performing a conditional update, you can update the item but only if the version number of the item stored in the table hasn’t changed. If there is a version mismatch, it means that the item got modified after you retrieved it but before you performed the update operation. As a result, the update attempt fails. If this happens, you can try again by repeating the process. See the following code: ddb = boto3.client('dynamodb') # Retrieve the latest version response_latest_version = ddb.get_item( TableName = 'VersionControl', Key = { 'PK': {'S': event['ID']}, 'SK': {'S': 'v0'} } ) latest_version = 0 higher_version = 1 # Extract 'Latest' from response if 'Item' in response_latest_version: latest_version = response_latest_version['Item']['Latest']['N'] higher_version = int(latest_version) + 1 # Transactional write where Update and Put are grouped together ddb.transact_write_items( TransactItems = [ { 'Update': { 'TableName': 'VersionControl', 'Key': { 'PK': { 'S': event['ID'] }, 'SK': { 'S': 'v0' } }, # Conditional write makes the update idempotent here # since the conditional check is on the same attribute # that is being updated. 'ConditionExpression': 'attribute_not_exists(#latest) OR #latest = :latest', 'UpdateExpression': 'SET #latest = :higher_version, #time = :time, #state = :state', 'ExpressionAttributeNames': { '#latest': 'Latest', '#time': 'Time', '#state': 'State' }, 'ExpressionAttributeValues': { ':latest': { 'N': str(latest_version) }, ':higher_version': { 'N': str(higher_version) }, ':time': { 'S': event['Time'] }, ':state': { 'S': event['State'] } } } }, { 'Put': { 'TableName': 'VersionControl', 'Item': { 'PK': { 'S': event['ID'] }, 'SK': { 'S': 'v' + str(higher_version) }, 'Time': { 'S': event['Time'] }, 'State': { 'S': event['State'] } } } } ] ) Conclusion When designing for an application, the requirements that you need to satisfy determine whether to choose a time-based or a number-based version. When you use a composite primary key for a table in DynamoDB, an entity item collection is modeled in such a way that you can easily retrieve the latest version while at the same time maintain the historical revisions. When you design and implement for a use case that requires version control, consider how frequent new revisions are added and how tolerant your use case is towards inconsistent versioning. About the Author Samaneh Utter is an Amazon DynamoDB Specialist Solutions Architect based in Göteborg, Sweden. https://aws.amazon.com/blogs/database/implementing-version-control-using-amazon-dynamodb/
0 notes