AWS CDK:一旦 lambda 函数检测到 URL 中的变化,就发送 SMS 消息
AWS CDK: Sending and SMS message once a lambda function detects changes in URL
我有一个 CDK 应用程序,它使用 lambda 函数来检查网站内容的更改。如果检测到更改,我希望将该短信发送到手机号码。
在我的 AWS CDK 项目中,我创建了我的堆栈,它构建了 lambda 函数和 SNS
文件:lib/lambda_query_website_aws_cdk-stack.ts
import * as cdk from '@aws-cdk/core';
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-sns';
import * as subscriptions from '@aws-cdk/aws-sns-subscriptions';
import fs = require('fs')
export class LambdaQueryWebsiteAwsCdkStack extends cdk.Stack {
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': ''
}
});
// Runs every 30 minutes from 6:00 to 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));
// Create an SNS Topic.
// The producer or publisher is the lambda function ??
//The subscriber or consumer is the sms phone number +15551231234
const myTopic = new sns.Topic(this, 'MyTopic');
myTopic.addSubscription(new subscriptions.SmsSubscription('+15551231234'));
}
}
这是 lambda 函数
文件 lambda-handler.py
import os
import urllib.request
url= "https://mywebsite.com"
def main(event, context):
print("I'm running!")
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:
# Send the SMS notifying about the change
如何让SNS服务知道消息需要发送?
BR
尝试这样的事情
export class LambdaQueryWebsiteAwsCdkStack extends cdk.Stack {
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) // Grant lambda function permission to publish to topic
// Runs every 30 minutes from 6:00 to 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"
client = boto3.client("sns")
SNS_TOPIC_ARN = os.getenv("SNS_TOPIC_ARN")
def main(event, context):
print("I'm running!")
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
response = client.publish(
TopicArn=SNS_TOPIC_ARN,
Message='There was a change on the web site',
Subject='Some change!'
)
我有一个 CDK 应用程序,它使用 lambda 函数来检查网站内容的更改。如果检测到更改,我希望将该短信发送到手机号码。
在我的 AWS CDK 项目中,我创建了我的堆栈,它构建了 lambda 函数和 SNS
文件:lib/lambda_query_website_aws_cdk-stack.ts
import * as cdk from '@aws-cdk/core';
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-sns';
import * as subscriptions from '@aws-cdk/aws-sns-subscriptions';
import fs = require('fs')
export class LambdaQueryWebsiteAwsCdkStack extends cdk.Stack {
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': ''
}
});
// Runs every 30 minutes from 6:00 to 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));
// Create an SNS Topic.
// The producer or publisher is the lambda function ??
//The subscriber or consumer is the sms phone number +15551231234
const myTopic = new sns.Topic(this, 'MyTopic');
myTopic.addSubscription(new subscriptions.SmsSubscription('+15551231234'));
}
}
这是 lambda 函数
文件 lambda-handler.py
import os
import urllib.request
url= "https://mywebsite.com"
def main(event, context):
print("I'm running!")
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:
# Send the SMS notifying about the change
如何让SNS服务知道消息需要发送?
BR
尝试这样的事情
export class LambdaQueryWebsiteAwsCdkStack extends cdk.Stack {
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) // Grant lambda function permission to publish to topic
// Runs every 30 minutes from 6:00 to 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"
client = boto3.client("sns")
SNS_TOPIC_ARN = os.getenv("SNS_TOPIC_ARN")
def main(event, context):
print("I'm running!")
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
response = client.publish(
TopicArn=SNS_TOPIC_ARN,
Message='There was a change on the web site',
Subject='Some change!'
)