如何将 shell 脚本 (json) 输出发送到 aws sns 主题?
How to send shell script (json) output to aws sns topic?
我在 AWS 控制台中创建了一个 arn 主题,还使用电子邮件创建了订阅并确认了订阅。
我在 AWS 中有一个 EC2 实例 运行ning,我写了一个小脚本来发送一些详细信息,例如实例类型、使用的操作系统、体系结构、JDK 安装并将其格式化为"json" 使用 "printf" 命令。
我想将脚本的 json 输出发送到我创建的主题。计划是在未来将这些东西自动化,但目前它只是一个测试目的。
我正在尝试的代码:
#/bin/bash
AWS_ACCESS_KEY=xUISTaZscgstTgsreTTS
AWS_SECRET_KEY=8hsggTjakjst86AHGSR98agHHYI
credentials="-I $AWS_ACCESS_KEY -S $AWS_SECRET_KEY"
topicARN=arn:aws:sns:eu-west-1:9876577867654:xyzsy-ex:9d18e516-25h3-81h1-n148-chqa712qi1p0
json="$(./temp.sh)"
aws sns publish --topic-arn $topicARN --message-structure json --message "${json}"
temp.sh 输出:
{
"component": {
"component": "xyz-name",
"comp_version": "03"
},
"Instance": {
"Instance_Type": "t2.small",
"Instance_ID": "i-1e993001234q56789"
"operatingsystem": "rhel"
},
"JDK": "1.8.0_111"
}
但它要求提供 AWS 区域,如果提供则抱怨凭证有效性。有没有办法动态提供凭据而不是设置 shell 从“.aws/config”中选择?
我打算 运行 它作为 cronjob,以便每 20 分钟将输出发送到主题。
来自 the docs:
The AWS CLI looks for credentials and configuration settings in the
following order:
- Command Line Options – region, output format and profile can be specified as command options to override default settings.
- Environment Variables –
AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, etc.
- The AWS credentials file – located at
~/.aws/credentials
on Linux, OS X, or Unix, or at C:\Users\USERNAME \.aws\credentials
on Windows.
This file can contain multiple named profiles in addition to a default
profile.
- The CLI configuration file – typically located at
~/.aws/config
on Linux, OS X, or Unix, or at C:\Users\USERNAME \.aws\config
on
Windows. This file can contain a default profile, named profiles, and
CLI specific configuration parameters for each.
- Instance profile credentials – these credentials can be used on EC2 instances with an assigned instance role, and are delivered through
the Amazon EC2 metadata service.
因此,您可以传递来自您调用的 aws sns
命令的凭据:
#/bin/bash
topicARN=arn:aws:sns:eu-west-1:9876577867654:xyzsy-ex:9d18e516-25h3-81h1-n148-chqa712qi1p0
json="$(./temp.sh)"
AWS_ACCESS_KEY=xUISTaZscgstTgsreTTS AWS_SECRET_KEY=8hsggTjakjst86AHGSR98agHHYI aws sns publish --topic-arn $topicARN --message-structure json --message "${json}"
我在 AWS 控制台中创建了一个 arn 主题,还使用电子邮件创建了订阅并确认了订阅。
我在 AWS 中有一个 EC2 实例 运行ning,我写了一个小脚本来发送一些详细信息,例如实例类型、使用的操作系统、体系结构、JDK 安装并将其格式化为"json" 使用 "printf" 命令。
我想将脚本的 json 输出发送到我创建的主题。计划是在未来将这些东西自动化,但目前它只是一个测试目的。
我正在尝试的代码:
#/bin/bash
AWS_ACCESS_KEY=xUISTaZscgstTgsreTTS
AWS_SECRET_KEY=8hsggTjakjst86AHGSR98agHHYI
credentials="-I $AWS_ACCESS_KEY -S $AWS_SECRET_KEY"
topicARN=arn:aws:sns:eu-west-1:9876577867654:xyzsy-ex:9d18e516-25h3-81h1-n148-chqa712qi1p0
json="$(./temp.sh)"
aws sns publish --topic-arn $topicARN --message-structure json --message "${json}"
temp.sh 输出:
{
"component": {
"component": "xyz-name",
"comp_version": "03"
},
"Instance": {
"Instance_Type": "t2.small",
"Instance_ID": "i-1e993001234q56789"
"operatingsystem": "rhel"
},
"JDK": "1.8.0_111"
}
但它要求提供 AWS 区域,如果提供则抱怨凭证有效性。有没有办法动态提供凭据而不是设置 shell 从“.aws/config”中选择?
我打算 运行 它作为 cronjob,以便每 20 分钟将输出发送到主题。
来自 the docs:
The AWS CLI looks for credentials and configuration settings in the following order:
- Command Line Options – region, output format and profile can be specified as command options to override default settings.
- Environment Variables –
AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
, etc.- The AWS credentials file – located at
~/.aws/credentials
on Linux, OS X, or Unix, or atC:\Users\USERNAME \.aws\credentials
on Windows. This file can contain multiple named profiles in addition to a default profile.- The CLI configuration file – typically located at
~/.aws/config
on Linux, OS X, or Unix, or atC:\Users\USERNAME \.aws\config
on Windows. This file can contain a default profile, named profiles, and CLI specific configuration parameters for each.- Instance profile credentials – these credentials can be used on EC2 instances with an assigned instance role, and are delivered through the Amazon EC2 metadata service.
因此,您可以传递来自您调用的 aws sns
命令的凭据:
#/bin/bash
topicARN=arn:aws:sns:eu-west-1:9876577867654:xyzsy-ex:9d18e516-25h3-81h1-n148-chqa712qi1p0
json="$(./temp.sh)"
AWS_ACCESS_KEY=xUISTaZscgstTgsreTTS AWS_SECRET_KEY=8hsggTjakjst86AHGSR98agHHYI aws sns publish --topic-arn $topicARN --message-structure json --message "${json}"