我有一個CDK應用程式,它使用一個lambda函式來檢查網站內容的變化。我希望在檢測到變化時向一個手機號碼發送短信。
在我的AWS CDK專案中,我創建了我的堆疊,該堆疊構建了lambda函式和SNS
檔案:lib.com
檔案:lib/lambda_query_website_aws_cdk-stack.ts 下面是lambda函式import * as cdk from '@aws-cdk/core'/span>;
import events = require('@aws-cdk/aws-events') 。
import targets = require('@aws-cdk/aws-events-targets')。
import lambda = require('@aws-cdk/aws-lambda')。
import * as sns from '@aws-cdk/aws-ns';
import * as subscriptions from '@aws-cdk/aws-sns-subscriptions'/span>;
import fs = require('fs')
export class LambdaQueryWebsiteAwsCdkStack extends cdk. 堆疊 {
constructor(scope: cdk.Construct, id: string, props? : cdk.StackProps) {
super(scope, id, props)。
const lambdaFn = new lambda.Function(this, 'Singleton', {
code: new lambda.InlineCode(fs. readFileSync('lambda-handler.py', { encoding: 'utf-8' })。
handler: 'index.main',
timeout: cdk.Duration.seconds(300)。
runtime: lambda.Runtime.PYTHON_3_6,
environment: {
'PREV_RESPONSE': ''。
}
});
//從6:00到23:00每30分鐘運行一次。
//見https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html。
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.expression('cron(0/30 6-23 * ? *) ' )
});
rule.addTarget(new targets.LambdaFunction(lambdaFn) )。
//創建一個SNS主題。
///生產者或發布者是lambda函式嗎?
//訂戶或消費者是短信電話號碼 15551231234。
const myTopic = new sns.Topic(this, 'MyTopic') 。
myTopic.addSubscription(new subscriptions.SmsSubscription(' 15551231234')) 。
}
import os
import urllib.request
url= "https://mywebsite.com"/span>
def main(event, context)。
print("我正在運行!")
response = urllib.request.urlopen(url)
response_text = response.read()
html = response_text.decode('utf-8')
if os.getenv('PREV_RESPONSE', default="HTML_BODY") != html:
print("There was a change on the web site")
os.environ['PREV_RESPONSE'] = HTML
# TODO:。
# 發送短信,通知有關變化。
我如何讓SNS服務知道需要發送訊息?
BR
uj5u.com熱心網友回復:
試試這樣的方法吧
export class LambdaQueryWebsiteAwsCdkStack extends cdk. 堆疊 {
constructor(scope: cdk.Construct, id: string, props? : cdk.StackProps) {
super(scope, id, props)。
const myTopic = new sns.Topic(this, 'MyTopic') 。
myTopic.addSubscription(new subscriptions.SmsSubscription(' 15551231234')) 。
const lambdaFn = new lambda.Function(this, 'Singleton', {
code: new lambda.InlineCode(fs. readFileSync('lambda-handler.py', { encoding: 'utf-8' })。
handler: 'index.main',
timeout: cdk.Duration.seconds(300)。
runtime: lambda.Runtime.PYTHON_3_6,
environment: {
'PREV_RESPONSE': ''。
'SNS_TOPIC_ARN': myTopic.topicArn //include topic ARN as environment variable.
}
});
myTopic.grantPublish(lambdaFn.role) //授予lambda函式向主題發布的權限。
///每30分鐘運行一次,從6:00到23:00。
// See https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html
const rule = new events.Rule(this, 'Rule', {
schedule: events.Schedule.expression('cron(0/30 6-23 * ? *) ' )
});
rule.addTarget(new targets.LambdaFunction(lambdaFn) )。
}
而在你的python函式中
import os
import boto3
import urllib.request
url= "https://mywebsite.com"/span>
client = boto3.client("sns")
SNS_TOPIC_ARN = os.getenv("SNS_TOPIC_ARN")
def main(event, context)。
print("我正在運行!")
response = urllib.request.urlopen(url)
response_text = response.read()
html = response_text.decode('utf-8')
if os.getenv('PREV_RESPONSE', default="HTML_BODY") != html:
os.environ['PREV_RESPONSE'] = html
回應 = client.publish(
TopicArn=SNS_TOPIC_ARN,
Message='There was a change on the web site',
主題='一些變化!'。
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332536.html
標籤:
