I’m trying to post my customer name using fetch, in an async function.
async function ExecuteEC2(customer) {
const response = await fetch(
'https:api',
{
method: 'POST',
body: {
Customer: customer,
},
success: function (response) {
alert(response.status);
},
error: function () {
alert("error with database");
}
}
);
}
The function is used in my button so that when it is clicked it triggers the API.
<button
className="btn btn-primary bt-btn btn-size"
type="button"
onClick={() => {
alert("Oh, hi " + customer);
ExecuteEC2(customer);
}}
>
Configure Ansible
</button>
<Modal
showModal={showModal}
setShowModal={setShowModal}
devices={options.AllCSRList}
user={user}
customer={customer}
/>
Finally, the lambda function has been set up to read and apply the API data to our EC2. However, it doesn’t show up whenever I try to grab the customer data. Where and how would I amend this to push it through the post?
import time
import json
import boto3
import urllib3
import requests
from pprint import pprint
region = 'eu-west-2'
instance = 'i-ec2'
ec2 = boto3.client('ec2', region_name=region)
def handler(event, context):
customer_name = event['Customer']
print(customer_name)
#boto3 client
client = boto3.client('ec2')
ssm = boto3.client('ssm')
# status = client.describe_instance_status(IncludeAllInstances = True)
# pprint(status)
# response = ssm.send_command(
# InstanceIds=['i-ec2'],
# DocumentName="AWS-RunShellScript",
# Parameters={'commands':[
# "mkdir create2.txt",
# ]},
# )
# print(response)
return {
'statusCode': 200,
'body': json.dumps(customer_name)
}
2
Answers
To fix this issue I had to redo my async function.
I first added the function to my API call file.
Following this, I then edited the async function in the deploypage.js to call what was needed,
This seemed to do the trick!
Note: I am writing my answer with respect to javascript version of lambda.
The post data is in the body property of the event object.
Also the the
body
is a json string and you will have to convert it to a json/dictionary.then