ãªãã¶ãŒãããªãã£ïŒå¯èŠ³æž¬æ§ïŒãšã¯ãã·ã¹ãã ã§äœãèµ·ãã£ãŠããããã©ãã ãææ¡ã§ãããã衚ããŸããå€ãã®å Žåãã¡ããªã¯ã¹ããã°ããã¬ãŒã¹ãåéããããã«èšè£
ããããšã§å®çŸããŸããéçšäœéšã®åäžããæ¥åç®æšã®éæãå®çŸããã«ã¯ãã·ã¹ãã ãã©ã®ããã«åäœããŠããããçè§£ããå¿
èŠããããŸãããããå®çŸããããã«ãå€ãã®ã客æ§ããªã¢ã«ã¿ã€ã ã®ã¢ãã¿ãªã³ã°ãã¢ã©ãŒããšã¢ã©ãŒã ãã«ã¹ã¿ãã€ãºå¯èœãªããã·ã¥ããŒãã®å©çšããããžãã¹ã®æææ±ºå®ãæ¯æŽãç®çãšããã¡ããªã¯ã¹ã®å¯èŠåãè¡ãããã«ã Amazon CloudWatch ãå©çšããŠããŸããçµç¹ã§ã¯ãã«ã¹ã¿ã ãã©ã¡ãŒã¿ã䜿çšããŠä»»æã® AWS Lambda 颿°ãåŒã³åºãããšãã§ãã CloudWatch ããã·ã¥ããŒããŠã£ãžã§ãããå©çšããããšãå¯èœã§ããããã«ãããããã·ã¥ããŒãäžã§ããŒã¿ãåæãããã³è¡šç€ºã§ããå¯èœæ§ã倧ããåºãããŸããããããçµç¹å
ã«ãããã®é¢æ°ãäœæããå°éç¥èããªãå Žåã¯ã©ãããã°è¯ãã§ãããããããã§ã Amazon CodeWhisperer ããããã®é¢æ°ã®äœæãæ¯æŽããã³ãŒãææ¡ãè¡ãããšã§ããªãã¶ãŒãããªãã£ã®åäžã«åœ¹ç«ã¡ãŸãã Amazon CloudWatch ããã·ã¥ããŒã ã䜿çšãããšã AWS ç°å¢å
šäœã®ããŸããŸãªã¡ããªã¯ã¹ãšãªãœãŒã¹ãäžå
çã«ã¢ãã¿ãªã³ã°ããããšãã§ããããã«ãªããŸããããã«ãããã·ã¹ãã ãçµç¹å
šäœã§ã®ã¢ããªã±ãŒã·ã§ã³ã®å¥å
šæ§ãšããã©ãŒãã³ã¹ãææ¡ããããšãã§ããŸããã«ã¹ã¿ã ãŠã£ãžã§ããã«ããããããã®ããã·ã¥ããŒãã®æè»æ§ã倧å¹
ã«åäžããé«åºŠãªã¬ãã«ã®ç£èŠãšå¯èŠåãå¯èœã«ãªããŸãã Amazon CodeWhisperer ã¯ãçµ±åéçºç°å¢ (IDE) ã§ãªã¢ã«ã¿ã€ã ã« 1 è¡ãŸãã¯é¢æ°ã³ãŒãå
šäœã®ææ¡ãçæããããšãã§ãã AI ãµãŒãã¹ã§ããããã«ããããœãããŠã§ã¢ã®æ§ç¯ãè¿
éã«è¡ãããšãã§ããŸãã ãã®æçš¿ã§ã¯ã CodeWhisperer ãå©çšããŠã ã«ã¹ã¿ã ãŠã£ãžã§ãã ã CloudWatch ããã·ã¥ããŒãã®éçºããã³ãããã€ã«äœ¿çšãããã³ãŒãã®äœæãæ¯æŽããæ¹æ³ã瀺ããŸãã ãŸããã®æçš¿ã§ã¯ AWS Cloud Development Kit (CDK) ã䜿çšããŸããCDK ã¯ãè¿ä»£çãªããã°ã©ãã³ã°èšèªã䜿çšããŠã€ã³ãã©ã¹ãã©ã¯ãã£ãã³ãŒããšããŠå®çŸ©ãã AWS CloudFormation ãéããŠãããã€ããããã®ãªãŒãã³ãœãŒã¹ã®ãœãããŠã§ã¢éçºãã¬ãŒã ã¯ãŒã¯ã§ãã ãœãªã¥ãŒã·ã§ã³å
šäœå ãã®ãœãªã¥ãŒã·ã§ã³ã§ã¯ã Amazon CodeWhisperer ã䜿çšããŠã CDK ã³ãŒãã®èšè¿°ã«é¢ããã³ãŒãææ¡ãè¡ããŸãã AWS CDK ã䜿çšããŠãããŸããŸãªã¿ã€ãã® 3 ã€ã®ãŠã£ãžã§ãããæã€ããã·ã¥ããŒããã³ãŒãã£ã³ã°ããã³ãããã€ããŸãã äŸã§ã¯ãããã¹ããŠã£ãžã§ãããš 2 ã€ã®ã«ã¹ã¿ã ãŠã£ãžã§ããã®äœææ¹æ³ã瀺ããŸãã 以äžã®å³ã¯ã CDK ãä»ããŠäœæããããªãœãŒã¹ã®æŠèŠã瀺ããŠããŸãã å³1. CDK ã«ãã£ãŠäœæããã Amazon CloudWatch ããã·ã¥ããŒãã¢ãŒããã¯ã㣠ãã®æçš¿ã®ãã¹ãŠã®æé ã«æ²¿ã£ãŠæäœãè¡ããšãCloudWatch ããã·ã¥ããŒãã¯æ¬¡ã®ããã«ãªããŸãã2 ã€ã®ã«ã¹ã¿ã ãŠã£ãžã§ãããš 1 ã€ã®ããã¹ããŠã£ãžã§ãããå«ãŸããŸãã å³2. CloudWatch ããã¹ããŠã£ãžã§ãã ãã®æçš¿å
šäœãéããŠè¡ãããšã¯ä»¥äžã®ãšããã§ãã Amazon CodeWhisperer ã®èšå® ãã«ã㌠ID ã䜿çšããããã« IDE ãèšå® AWS CLI ã AWS CDK ã AWS IAM ã®èšå® Amazon CodeWhisperer ã䜿çšã㊠CDK ã³ãŒããš Lambda 颿°ã®äœæ ãããã€ã®ããã®ã¢ã»ãããä¿æãã Amazon Simple Storage Service(S3) ãã±ããã®äœæ ããã·ã¥ããŒããšã«ã¹ã¿ã ãŠã£ãžã§ããã®ãããã€ åææ¡ä»¶ ãã®æçš¿ã«æ²¿ã£ãŠäœæ¥ãé²ããå ŽåãããŒã«ã«ãã·ã³äžã®çµ±åéçºç°å¢ (IDE) ã§äœæ¥ãè¡ãªã£ãŠãããŸãã CodeWhisperer ã¯ã Visual Studio Code (VS Code) ã PyCharm ãªã©ã® JetBrains 補åãªã©ãè€æ°ã® IDE ã§åäœããŸãããã®èšäºã§ã¯ VS Code ã䜿çšããŸããä»ã® IDE ã®äœ¿çšæé ã¯ã Amazon CodeWhisperer ã®ããã¥ã¡ã³ã ã§ç¢ºèªã§ããŸãã ãŸãã以äžã«ã€ããŠãå¿
èŠã«ãªããŸãã AWS ã¢ã«ãŠã³ã Visual Studio Code Python ãš pip AWS CLI AWS CDK ãã«ã㌠ID ã䜿çšããããã« IDE ãèšå® AWS Builder ID ã䜿çšããŠã VS Code ã§ CodeWhisperer ãèšå®ããŸããããAWS Builder ID ã¯ã AWS äžã§éçºãããã¹ãŠã®ãŠãŒã¶ãŒã®ããã®æ°ããå人ãããã¡ã€ã«ã§ãã ãŸãæåã«ãAWS Toolkit ãã€ã³ã¹ããŒã«ãããŠããããšã確èªããå¿
èŠããããŸãã VS Code IDE ã®å·ŠåŽã®ããã«ã§ [Extensions] ã«ç§»åããŸããæ€çŽ¢ããŒã«ãAWS Toolkitããšå
¥åãã以äžã®ããã«æ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããŠãã ããã å³3. VS Codeã§ã®AWSæ¡åŒµæ©èœã®èšå® ããã§ãå·ŠåŽã®ããã«ã« AWS Toolkit ã¢ã€ã³ã³ã衚瀺ãããã¯ãã§ãã Visual Studio Code ã® AWS Toolkit ã§ã[Developer tools] ã® [CodeWhisperer] ãéžæãã [Start] ãã¯ãªãã¯ããŸãããããšã VS Code ã®äžéšã«ããããããŠã³ã¡ãã¥ãŒã衚瀺ãããŸãã å³4. CodeWhispererã®éå§ ããããããŠã³ã¡ãã¥ãŒããã [Use a personal email to sign up and sign in with AWS Builder ID] ãéžæããŸãã å³5. å人ã¡ãŒã«ã¢ãã¬ã¹ã®èšå® [Copy Code for AWS Builder ID] ã®ããã³ããã衚瀺ããããã [Copy Code] ãš [Proceed] ãéžæããŸãã [Do you want Code to open the external website?] ã®ããã³ããã衚瀺ããããã [Open] ãéžæããŸãã ãã©ãŠã¶ã¿ããéãã [Authorize request] ãŠã£ã³ããŠã衚瀺ãããŸããã³ãŒãã¯ãã§ã«ã¯ãªããããŒãã«ã³ããŒãããŠããã¯ãã§ããããã貌ãä»ããŠã [Next] ãéžæããŸãã [Create AWS Builder ID] ããŒãžããã©ãŠã¶ã¿ãã§éããŸããã¡ãŒã«ã¢ãã¬ã¹ãå
¥åãã [Next] ãéžæããŸãã [Your name] ã®ãã£ãŒã«ãã衚瀺ãããã®ã§ãååãå
¥åãã [Next] ãéžæããŸãã å
¥åããã¡ãŒã«ã¢ãã¬ã¹ã«ç¢ºèªã³ãŒããå±ããŸãã確èªç»é¢ã§å±ããã³ãŒããå
¥åãã [Verify] ãéžæããŸãã [Choose your password] ç»é¢ã§ããã¹ã¯ãŒããå
¥åãã確èªå
¥åããŠããã [Create AWS Builder ID] ãéžæããŸãã ãã©ãŠã¶ã¿ããéãã Visual Studio Code ã® AWS Toolkit ã«ããŒã¿ã«ã¢ã¯ã»ã¹ããããšãèš±å¯ããããæ±ããã¡ãã»ãŒãžã衚瀺ãããŸãã [Allow] ãéžæããŸãã VS Code ã«æ»ããŸãããã§ã« IAM ã䜿çšããŠä»ã® AWS ããŒã«ã«èªèšŒããŠããå Žåã¯ããã¹ãŠã® AWS ãµãŒãã¹ã§ Builder ID ã䜿çšãããã©ãããå°ããããŸããç¶æ³ã«é©ãããªãã·ã§ã³ãéžæããŸãã AWS CLI ã AWS CDK ã AWS IAM ã®èšå® ãã®ã»ã¯ã·ã§ã³ã§ã¯ãAWS CLIãCDKãCDKã®ãã©ã«ãæ§é ãèšå®ããŸãã AWS CLIãèšå®ããã«ã¯ãã AWS CLI ãèšå®ãã ãã®æé ã«åŸã£ãŠãã ããã æ°ãããã©ã«ããäœæãã sample1 ãšååãä»ããŸãããã®ãã©ã«ãã§ CDK ãããžã§ã¯ããäœæããŸãã sample1 ãã©ã«ãã«ãã£ã¬ã¯ããªã倿ŽããŸãã cd sample1 cdk ãããŒãã¹ãã©ããããŸãã cdk bootstrap aws://< ã¢ã«ãŠã³ãçªå· >/< ãªãŒãžã§ã³å > cdk python ç°å¢ãäœæããŸãã cdk init --language python ããã«ãããsample1 ãã©ã«ãã«å¿
èŠãª python cdk ãã¡ã€ã«ãäœæãããŸãã venv ãã¢ã¯ãã£ããŒãããŸãã source .venv/bin/activate å¿
èŠãªããã±ãŒãžãã€ã³ã¹ããŒã«ããŸãã pip install -r requirements.txt Amazon CodeWhisperer ã䜿çšããã³ãŒãã®äœæ ãã®ã»ã¯ã·ã§ã³ã§ã¯ã CodeWhisperer ã䜿çšã㊠CDK ã³ãŒããèšè¿°ããæ¹æ³ã説æããŸãã CDK ã䜿çšããŠã次㮠6 ã€ã®ãªãœãŒã¹ãäœæããæ¹æ³ã説æããŠãããŸãã 2 ã€ã® Lambda 颿° 1 ã€ã® Lambda 颿°ã¯ããŠãŒã¶ãŒå
¥åãåãåãã察å¿ããåºåãçæããŸãã ãã 1 ã€ã®Lambda颿°ã¯ã S3 ãã±ãããã HTML ãååŸããŸãã 3 ã€ã® CloudWatch ãŠã£ãžã§ãã 1 ã€ç®ã® Lambda 颿°ãšçµ±åããã«ã¹ã¿ã ãŠã£ãžã§ããããŠãŒã¶ãŒå
¥åãåãä»ãã Lambda 颿°ããããŒã¿ãèªã¿åã£ãŠåºåãæäŸããŸãã 2 ã€ç®ã® Lambda 颿°ãšçµ±åããã«ã¹ã¿ã ãŠã£ãžã§ããã S3 ãã±ãããã HTML ãã¡ã€ã«ãèªã¿åããŸãã ãHello Worldããšè¡šç€ºããããã¹ããŠã£ãžã§ãã 1 ã€ã® CloudWatch ããã·ã¥ããŒã Visual Studio Codeã®å©çš å·Šäžã® [File] ã¡ãã¥ãŒããã [Open Folder] ãéžæããŸããïŒã¹ããã 2 ã§äœæãã sample1 ãã©ã«ããéžæããŸããïŒ assets ãšããååã®æ°ãããã©ã«ããäœæãã HelloWorld.html ãšããååã® html ãã¡ã€ã«ãäœæããŸãã <!DOCTYPE html> <html> <body> <h1>Hello!! This is an HTML file from S3 bucket</h1> </body> </html> ãã®ãã¡ã€ã«ã¯ S3 ãã±ããã«ã¢ããããŒãããã Lambda 颿°ã«ãã£ãŠèªã¿èŸŒãŸããŸãã åã³ sample1 ãã©ã«ãã«ç§»åãã sample1 ãšããååã®ãµããã©ã«ããæ¢ããŸãã ãã®ãµããã©ã«ãã«ããã¹ã¿ãã¯ãã¡ã€ã«ãç·šéããŸãã ã»import æã远å ããŸãïŒå
¥åæã« CodeWhisperer ãèªåè£å®ãæ¯æŽããŸãïŒ from aws_cdk import ( aws_lambda, Stack, App, Duration, aws_iam, aws_cloudwatch, aws_s3, RemovalPolicy, aws_s3_deployment ) å¿
èŠãªã¢ãžã¥ãŒã«ãã€ã³ããŒãããããæ¬¡ã®è¡ãæ¢ããŸãã # The code that defines your stack goes here ãã®è¡ãåé€ããã¹ã¿ãã¯ã³ãŒããèšè¿°ããŸãã CodeWhisperer ã«ã³ã¡ã³ããå
¥åããŠã AWS CDK ã䜿çšããæåã® Lambda 颿°ã®äœæãææ¡ãããŸãã # define lambda function that has inline code å
¥åãéå§ãããšã CodeWhisperer ãææ¡ãéå§ããŸãã Lambda 颿°ã®äœæã«é¢ãããã«ãã衚瀺ããããã Lambda 颿°ã® CDK ã³ãŒããæ¬¡ã®ããã«èšè¿°ããŸãã # define lambda function that has inline code lambda_function = aws_lambda.Function(self, 'HelloWorldFunction', code=aws_lambda.Code.from_inline(""" def lambda_handler(event, context): showme = event['widgetContext']['forms']['all'].get('petType') # create a 2D array for pets and count data = [['puppy','10'],['bunny','5'],['kitten','6']] rowdata = "" for row in data: if (showme and showme==row[0]): # assume only 2 elements in the data rowdata += f''' <tr> <td>{row[0]}</td> <td>{row[1]}</td> </tr>''' response = f''' <p>Which pet do you want to see? valid inputs: puppy/bunny/kitten <form><input name="petType" value="{showme}" size="10"></form> </p> <table> <tr> <th>Pets</th> <th>Count</th> </tr> {rowdata} </table> <a class="btn btn-primary">Run query</a> <cwdb-action action="call" endpoint="{context.invoked_function_arn}"></cwdb-action> ''' return response """ ), description="CloudWatch Custom Widget sample: simple hello world", function_name=self.stack_name, handler='index.lambda_handler', memory_size=128, runtime=aws_lambda.Runtime.PYTHON_3_9, timeout=Duration.seconds(60)' ) ã³ãŒããã«ã¹ã¿ãã€ãºããããã«å
¥åã远å ãããšã CodeWhisperer ãææ¡ãè¡ãããšã«æ°ã¥ãã§ãããããã®äŸã§ã¯ããããã®æ°ã® 2 次å
é
åãäœæãããŠãŒã¶ãŒã«ååŸããããŒã¿ãå
¥åããããæ±ããŠããŸãããŠãŒã¹ã±ãŒã¹ã«åºã¥ããŠããŒã¿ã倿Žã§ããŸãã æåã® CloudWatch ã«ã¹ã¿ã ãŠã£ãžã§ãããäœæããããã®ã³ã¡ã³ãã®å
¥åãéå§ããŸãããã®ãŠã£ãžã§ããã§ã¯ãå
¥åãèŠæ±ããåºåãšããŠåç¬ã®æ°ãæäŸããã³ãŒãã䜿çšããŸãã # define custom widget to display lambda function CodeWhisperer ã®ææ¡ã䜿çšããŠãã«ã¹ã¿ã ãŠã£ãžã§ãããäœæãã CDK ã³ãŒããèšè¿°ããŸãã # define custom widget to display lambda function my_custom_widget = aws_cloudwatch.CustomWidget( function_arn=lambda_function.function_arn, title="Hello, World!") CloudWatch ã®ããã¹ããŠã£ãžã§ãããäœæããããã®ã³ã¡ã³ãã®å
¥åãéå§ããŸãã # define a text widget that displays "Hello World" CodeWhisperer ã®ææ¡ãå©çšããªãããããã¹ããŠã£ãžã§ããã®ã³ãŒãã宿ãããŸãã # define a text widget that displays "Hello World" my_text_widget = aws_cloudwatch.TextWidget(markdown='# Hello World', width=6, height=2 ) assets ãã©ã«ãã®ãã¡ã€ã«ãããŒããã S3 ãã±ãããäœæããŸãããã # define parameter for s3 bucket name s3_bucket_name = f'helloworld-{self.account}' s3_file_name = 'HelloWorld.html' # define s3 bucket with account id as suffix for hello world with no public access s3_bucket = aws_s3.Bucket(self, "S3Bucket", versioned=False, block_public_access=aws_s3.BlockPublicAccess.BLOCK_ALL, encryption=aws_s3.BucketEncryption.KMS_MANAGED, auto_delete_objects=True, removal_policy=RemovalPolicy.DESTROY, bucket_name=s3_bucket_name ) # define s3 bucket deployment to deploy files under assets folder aws_s3_deployment.BucketDeployment(self, "S3BucketDeployment", sources=[aws_s3_deployment.Source.asset("assets")], destination_bucket=s3_bucket ) S3 ãã±ãããããã¡ã€ã«ãèªã¿åã第 2 ã® Lambda 颿°ãäœæããŸããããé¢é£ãã IAM ããŒã«ãäœæããŸãã # define IAM role for lambda that allows to read s3_file_name from s3 bucket with name s3_bucket_name s3GetObjectLambda_IAM_role = aws_iam.Role(self, "S3GetObjectLambda_IAM_role", assumed_by=aws_iam.ServicePrincipal("lambda.amazonaws.com"), inline_policies={ "S3GetObjectLambda_IAM_role_policy": aws_iam.PolicyDocument( statements=[ aws_iam.PolicyStatement( actions=["s3:GetObject"], effect=aws_iam.Effect.ALLOW, resources=[ f'arn:aws:s3:::{s3_bucket_name}/{s3_file_name}' ] ) ] ) } ) 2 ã€ç®ã®ã«ã¹ã¿ã ãŠã£ãžã§ãããäœæããŸãããã # define s3GetObject custom widget to display s3GetObjectlambda_function s3GetObject_custom_widget = aws_cloudwatch.CustomWidget( function_arn=s3GetObjectlambda_function.function_arn, width=6, height=4) 次ã«ãäœæãã 3 ã€ã®ãŠã£ãžã§ããããã¹ããã CloudWatch ããã·ã¥ããŒããäœæããŸããã³ã¡ã³ãã§ CodeWhisperer ã«ææ¡ãããããšã§ãããã·ã¥ããŒãã®ã³ãŒããäœæããŸãã # define cloudwatch dashboard CodeWhisperer ã®ã³ãŒãææ¡ã衚瀺ããããã以åã«äœæãããŠã£ãžã§ããã䜿çšããããã·ã¥ããŒãã®ã³ãŒãã宿ãããŸãã # define CloudWatch dashboard dashboard = aws_cloudwatch.Dashboard( self, 'HelloWorldDashboard', dashboard_name='HelloWorldDashboard' ) æåŸã«ãäœæãã 2 ã€ã®ã«ã¹ã¿ã ãŠã£ãžã§ãããš 1 ã€ã®ããã¹ããŠã£ãžã§ãããããã·ã¥ããŒãã«è¿œå ããŸãã # add the 3 widgets to the dashboard dashboard.add_widgets(my_custom_widget) dashboard.add_widgets(my_text_widget) dashboard.add_widgets(s3GetObject_custom_widget) ã³ãŒãã¯æ¬¡ã®äŸã®ããã«ãªãã¯ãã§ãã from aws_cdk import ( Stack, Duration, aws_lambda, aws_cloudwatch, aws_s3, RemovalPolicy, aws_iam, aws_s3_deployment ) from constructs import Construct import os class Sample1Stack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) cwd = os.getcwd() # define lambda function that has inline code lambda_function = aws_lambda.Function(self, 'HelloWorldFunction', code=aws_lambda.Code.from_inline( """ def lambda_handler(event, context): showme = event['widgetContext']['forms']['all'].get('petType') # create a 2D array for pets and count data = [['puppy','10'],['bunny','5'],['kitten','6']] rowdata = "" for row in data: if (showme and showme==row[0]): # assume only 2 elements in the data rowdata += f''' <tr> <td>{row[0]}</td> <td>{row[1]}</td> </tr>''' response = f''' <p>Which pet do you want to see? valid inputs: puppy/bunny/kitten <form><input name="petType" value="{showme}" size="10"></form> </p> <table> <tr> <th>Pets</th> <th>Count</th> </tr> {rowdata} </table> <a class="btn btn-primary">Run query</a> <cwdb-action action="call" endpoint="{context.invoked_function_arn}"></cwdb-action> ''' return response """ ), # code=aws_lambda.Code.from_asset(os.path.join(cwd, "lambdafolder/package.zip")) description="CloudWatch Custom Widget sample: simple hello world", function_name=self.stack_name, handler='index.lambda_handler', memory_size=128, runtime=aws_lambda.Runtime.PYTHON_3_9, timeout=Duration.seconds(60) ) # define a CustomWidget that uses the Lambda function my_custom_widget = aws_cloudwatch.CustomWidget( function_arn=lambda_function.function_arn, title="Hello, World!") # define a text widget that displays "Hello World" my_text_widget = aws_cloudwatch.TextWidget(markdown='Hello World', width=6, height=2 ) # define parameter for s3 bucket name s3_bucket_name = f'helloworld-{self.account}' s3_file_name = 'HelloWorld.html' # define s3 bucket with account id as suffix for hello world with no public access s3_bucket = aws_s3.Bucket(self, "S3Bucket", versioned=False, block_public_access=aws_s3.BlockPublicAccess.BLOCK_ALL, encryption=aws_s3.BucketEncryption.KMS_MANAGED, auto_delete_objects=True, removal_policy=RemovalPolicy.DESTROY, bucket_name=s3_bucket_name ) # define s3 bucket deployment to deploy files under assets folder aws_s3_deployment.BucketDeployment(self, "S3BucketDeployment", sources=[aws_s3_deployment.Source.asset("assets")], destination_bucket=s3_bucket ) # define IAM role for lambda that allows to read s3_file_name from s3 bucket with name s3_bucket_name s3GetObjectLambda_IAM_role = aws_iam.Role(self, "S3GetObjectLambda_IAM_role", assumed_by=aws_iam.ServicePrincipal("lambda.amazonaws.com"), inline_policies={ "S3GetObjectLambda_IAM_role_policy": aws_iam.PolicyDocument( statements=[ aws_iam.PolicyStatement( actions=["s3:GetObject"], effect=aws_iam.Effect.ALLOW, resources=[ f'arn:aws:s3:::{s3_bucket_name}/{s3_file_name}' ] ) ] ) } ) # define lambda function S3GetObject s3GetObjectlambda_function = aws_lambda.Function(self, "S3GetObjectLambdaFunction", #inline code to read file from s3 bucket code=aws_lambda.Code.from_inline(""" import boto3 import json import os s3 = boto3.client('s3') def lambda_handler(event, context): #read environment variable S3_BUCKET_NAME bucket = os.environ['S3_BUCKET_NAME'] key = os.environ['S3_FILE_NAME'] response = s3.get_object(Bucket=bucket, Key=key) file_content = response['Body'].read().decode('utf-8') return file_content """ ), runtime=aws_lambda.Runtime.PYTHON_3_9, handler="index.lambda_handler", timeout=Duration.seconds(60), memory_size=128, description="Lambda function that reads file from S3 bucket and returns file content", # define IAM role for S3GetObject role=s3GetObjectLambda_IAM_role, #environment variables environment={ 'S3_BUCKET_NAME': s3_bucket_name, 'S3_FILE_NAME': s3_file_name }, # define function name as s3GetObject function_name=f's3GetObject-{self.stack_name}' ) # define s3GetObject custom widget to display s3GetObjectlambda_function s3GetObject_custom_widget = aws_cloudwatch.CustomWidget( function_arn=s3GetObjectlambda_function.function_arn, width=6, height=4, title="S3 Get Object" ) # define a CloudWatch dashboard dashboard = aws_cloudwatch.Dashboard( self, 'HelloWorldDashboard', dashboard_name='HelloWorldDashboard' ) # add the 3 widgets to the dashboard dashboard.add_widgets(my_custom_widget) dashboard.add_widgets(my_text_widget) dashboard.add_widgets(s3GetObject_custom_widget) ã³ãŒããæ€èšŒããŸãã Visual Studio Code ã®ã¿ãŒããã«ãŠã£ã³ããŠã§æ¬¡ã®ã³ãã³ããå®è¡ããŸãã cdk ls ã³ãŒãããããã€ããŸãã Visual Studio Code ã®ã¿ãŒããã«ãŠã£ã³ããŠã§æ¬¡ã®ã³ãã³ããå®è¡ããŸãã cdk deploy ããã§æ¬¡ã®ãªãœãŒã¹ããããã€ãããŠããã¯ãã§ãã 2 ã€ã® Lambda 颿° 3 ã€ã®ãŠã£ãžã§ãããæã€ CloudWatch ããã·ã¥ããŒã 1 ã€ã®ããã¹ããŠã£ãžã§ãã Lambda ã«æ¥ç¶ããã 2 ã€ã® CustomWidget S3 ã® HTML ãã¡ã€ã«ããèªã¿åãã 1 ã€ã®ã«ã¹ã¿ã ãŠã£ãžã§ãã ãŠãŒã¶ãŒå
¥åãèŠæ±ãã Lambda ããé¢é£å€ãååŸãã 1 ã€ã®ã«ã¹ã¿ã ãŠã£ãžã§ãã ä»ã®ãŠã£ãžã§ããã®äœææ¹æ³ã¯ ãã¡ã ãåèã«ããŠãã ããã ç°å¢ã®åé€ ä»åŸèª²éãããªãããã«ããã«ã¯ãVisual Studio Code ã®ã¿ãŒããã«ãŠã£ã³ããŠã§æ¬¡ã®ã³ãã³ããå®è¡ããŠãªãœãŒã¹ãåé€ããŸãã cdk destroy ãŸãšã ãã®æçš¿ã§ã¯ã Amazon CodeWhisperer ã䜿çšã㊠CloudWatch ããã·ã¥ããŒããäœæãã CDK ã³ãŒããèšè¿°ããæ¹æ³ãåŠã³ãŸãããã芧ã®ãšããã CodeWhisperer ã¯éçºè
ã®çç£æ§ãåäžãããã ãã§ãªããããéãææãåºãããœãããŠã§ã¢éçºãå éããéçºåŽåãåæžããææ¡ãè¡ããã¢ã€ãã¢åµåºãè€éãªåé¡è§£æ±ºã®ããã®æéã確ä¿ã§ãããªã©ãå€ãã®ã¡ãªããããããŸãã詳现㯠CodeWhisperer ã®ããã¥ã¡ã³ã ãã芧ãã ããã æ¬èšäºã¯ãã Leverage generative AI to create custom dashboard widgets in Amazon CloudWatch using Amazon CodeWhisperer ãã翻蚳ãããã®ã§ãã翻蚳㯠ãœãªã¥ãŒã·ã§ã³ã¢ãŒããã¯ã接é·ãæ
åœããŸããã